一些优秀的 Swift 扩展,由 GoodRequest 团队精心制作。
要求
- iOS 10.0+
- Xcode 10.0+
- Swift 4.2(对于 Swift 4.0 使用版本 0.9.0)
安装
.goodswift 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod 'GoodSwift'
使用
映射
.goodswift 允许您使用 Unbox 解码器轻松地从 Alamofire 响应解码 JSON 对象。您可以在 Unbox 读写说明 中查看如何映射您的模型。
import Unbox
struct Foo {
let origin: String
let url: URL
}
extension Foo: Unboxable {
init(unboxer: Unboxer) throws {
self.origin = try unboxer.unbox(key: "origin")
self.url = try unboxer.unbox(key: "url")
}
}
然后,您只需使用 unbox
或 unboxArray
函数来解码您的模型。
import Alamofire
import GoodSwift
Alamofire.request("https://httpbin.org/get").unbox(completion: { (response: DataResponse<Foo>) in
switch response.result {
case .success(let foo):
// Decoded object
case .failure(let error):
// Handle error
}
})
日志记录
项目“构建设置”中定义了具有 Active Compilation Conditions
标志 DEBUG
时启用自动日志记录。如果您已使用CocoaPods 添加了 .goodswift,则需要手动将标志 DEBUG
添加到 .goodswift pod 的 构建设置
中的 Active Compilation Conditions
。如果您不想在每次 pod install
后手动添加此标志,只需将此脚本添加到您的 Podfile
文件的末尾。
post_install do |installer|
installer.pods_project.targets.each do |target|
if target.name == 'GoodSwift'
target.build_configurations.each do |config|
if config.name == 'Debug'
config.build_settings['SWIFT_ACTIVE_COMPILATION_CONDITIONS'] = 'DEBUG'
else
config.build_settings['SWIFT_ACTIVE_COMPILATION_CONDITIONS'] = ''
end
end
end
end
end
日志级别
您可以通过从 DataRequest
类设置 logLevel
静态变量来选择日志级别。目前,您可以从中选择以下日志级别:
- 错误 - 只有在发生错误时才打印
- 信息(默认) - 在发生错误时打印请求 URL、响应状态和错误
- 详细 - 打印所有内容,包括请求体和响应对象
链式动画
AnimationChain
允许您轻松链式链接 UIView
动画。
UIView.animationChain.animate(withDuration: 2) {
view.alpha = 0.0
}.animate(withDuration: 2) {
view.alpha = 1.0
}.start {
debugPrint("Animation finished")
}
链表实现
.goodswift 允许您使用默认的 LinkedList
实现(队列,FIFO)。请参阅 Wiki
let queue = LinkedList<Int>()
print(queue.isEmpty) // true
queue.push(1) // [1]
queue.push(2) // [1, 2]
queue.push(3) // [1, 2, 3]
print(queue.contains(1)) // true
print(queue.filter { $0 > 1 }) // [2, 3]
print(queue.map { $0 + 2 }) // [3, 4, 5]
print(queue.pop()) // Optional(1)
print(queue.pop()) // Optional(2)
print(queue.isEmpty) // false
print(queue.peak()) // Optional(3)
print(queue.pop()) // Optional(3)
print(queue.isEmpty) // true
作者
马雷克·斯佩勒克, [email protected]
贡献者
帕沃尔·科梅特, [email protected]
托马斯·吉巴什, [email protected]
多米尼克·佩托, [email protected]
菲利普·萨萨拉, [email protected]
许可证
.goodswift 在MIT许可证下可用。更多信息请参阅LICENSE文件。