这个库目的是IntrepidPursuits的工程师们制作的Swift方法的通用集合。随着库的发展,有用的组件将根据适当的需求,隔离到独立的podspec中。
采用这样的方法是为了让材料添加起来更方便,鼓励组件化和共享通用功能。
iOS版本
- iOS <= 9 -> <
0.11.0
Swift版本
- Swift 5.0 ->
0.13.0
+ - Swift 4.2 ->
0.11.0
through0.12.0
- Swift 4.1.50 (Xcode 10兼容) ->
0.10.3
- Swift 4.0 ->
0.9.0
through0.10.x
- Swift 3.2 (Xcode 9兼容) ->
0.8.3
through0.8.4
- Swift 3 ->
0.6.1
through0.8.1
- Swift 2 -> anything through
0.5.2
核心
pod 'Intrepid'
子规格
pod 'Intrepid/Rx' # Intrepid's RxSwift Extensions
测试补充
source 'https://github.com/IntrepidPursuits/swift-wisdom.git'
target 'YourTestTarget',
use_frameworks!
pod 'IntrepidSwiftWisdomTesting'
end
组件
Qu
Swift 中调度操作的简单封装。语法示例
Qu.Background {
// Sleep for long operation
sleep(4)
print("1")
} .Also {
sleep(4)
print("2")
} .Also {
sleep(1)
print("3")
} .Also {
sleep(1)
print("4")
} .Also {
sleep(1)
print("5")
} .ThenAfter(.previous(3)) {
print("6: After 5, 4, & 3")
} .Then {
sleep(1)
print("7: After 6")
} .FinallyOn(.main) {
sleep(1)
print("Finished: After All")
}
After
执行或重复未来操作的一种简单方法
After(2.5) {
print("Two and a half seconds later")
}
RepeatAtInterval(1.0, numberOfTimes: 5) {
print("Once a second, 5 times")
}
Nib Initable
从 nib 加载视图
let myCustomView = MyCustomView.fromNib()
颜色描述符
设置简单的配色方案。注意,如果您使用Zeplin,它会自动生成您想要的配色方案
enum ColorPalette : ColorDescriptor {
case White = "254,216,130,255"
case DarkGreen = "51,58,24,255"
case DarkGray = "64,48,56,255"
case BrightWhite = "#ffffff"
var color: UIColor {
return rawValue.color
}
}
并且使用
someView.backgroundColor = ColorPalette.White.color
单元格配置
一种简单的方法,用于覆盖大多数表格视图注册/挂起
注册
tableView.ip_registerCell(YourCell.self)
tableView.ip_registerHeader(YourHeader.self)
挂起
let cell: YourCell = tableView.ip_dequeueCell(indexPath)
//
let header: YourHeader = tableView.ip_dequeueHeader(section)
用户默认值
一种简单且类型安全地从默认值中读取和写入的方法
定义
enum Setting : String, EnumSettingsKeyAccessible {
case DisplayName
case LastOpenDate
}
读取
let displayName: String? = Setting.DisplayName.readFromDefaults
写入
Setting.DisplayName.writeToDefaults(displayName)
实施
struct ApplicationSettings {
var displayName: String {
get {
return Setting.DisplayName.readFromDefaults ?? ""
}
set {
Setting.DisplayName.writeToDefaults(newValue)
}
}
}
结果
与使用类似Objective-C样式的异步完成块(如(possibleObject: AnyObject?, error: NSError?)
)相比,结果类型更受欢迎。
func fetchName(completion: Result<String> -> Void)
并且使用
fetchName { result in
switch result {
case let .Success(name):
print("Successfully got name: \(name)")
case let .Failure(error):
print("Failed to get name: \(error)")
}
}
一天中的时间
用于将时间转换为从NSDate和到NSDate
转换为时间
let date = ...
let timeOfDay = TimeOfDay(date)
// or
let timeOfDay = TimeOfDay("1:30")
转换为日期
let timeOfDay = ...
// Time Today
let todayAtThatTime = timeOfDay.timeToday()
// Time on given date
let someDate = ...
let timeOnThatDate = timeOfDay.timeOnDate(someDate)
贡献
欢迎所有贡献和更新!以下是一些提交新增内容时遵循的基本准则
- 通过拉取请求(Pull Request)提交新增内容,并对其进行记录
- 遵循适当的文件夹约定
- 所有扩展方法和变量都以
ip_
前缀命名以避免命名空间问题。 - 如果功能模糊,请记录其功能。
- 合并前在分支上升级podspec并打标签
- 批准后,使用命令
pod trunk push Intrepid.podspec
将podspec推送到trunk。