Closures
是一个iOS框架,它为许多流行的UIKit和Foundation类添加了 闭包 处理程序。尽管这个框架可以替代一些Cocoa Touch设计模式,比如 委托 & 数据源 和 目标-动作,但作者并没有宣称哪种方法是 更好 的。大多数情况下,这只是风格、偏好或便利性的问题,这些问题将决定这些闭包扩展是否有益。
无论您是功能纯粹主义者,不喜欢特定的API,还是仅仅是想稍微组织一下代码,您可能都会喜欢使用这个库。
用法概述
方便的闭包
有时,您可能只想用闭包来处理 UIControl 的目标-动作。
button.onTap {
// UIButton tapped code
}
mySwitch.onChange { isOn in
// UISwitch value changed code
}
添加一个 手势识别器 可以压缩到一个方法中。
view.addPanGesture() { pan in
// UIPanGesutreRecognizer recognized code
}
使用数组填充视图?我来了。
tableView.addElements(myArray, cell: MyTableViewCell.self) { element, cell, index in
cell.textLabel!.text = "\(element)"
}
collectionView.addFlowElements(myArray, cell: MyCustomCollectionViewCell.self) { element, cell, index in
cell.myImageViewProperty.image = element.thumbImage
}
pickerView.addStrings(myStrings) { title, component, row in
// UIPickerView item selected code
}
菊花链
几乎所有便利方法都支持使用方法链。这允许我们在以一种简洁的方式实现可选的委托方法的同时,拥有一些不错的语法糖。以UITextField为例,我们可以组织和可视化所有的UITextFieldDelegate
行为。
textField
.didBeginEditing {
// UITextField did begin editing code
}.shouldClear {
true
}.shouldChangeCharacters { range, string in
// some custom character change code
return false
}
保留控制
您任何时候都不必局限于使用这些便利方法。例如,UITableView不需要填充数组。您也可以提供自己的UITableViewDelegate
和UITableViewDataSource
处理程序。
tableView.register(MyTableViewCell.self, forCellReuseIdentifier: "Cell")
tableView
.numberOfRows { _ in
myArray.count
}.cellForRow { indexPath in
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel!.text = myArray[indexPath.row]
return cell
}.didSelectRowAt { indexPath in
// IndexPath selected code
}
您不受想实施哪些委托/数据源方法的限制。同样,您可以操作任何UIControl事件。
anyControl.on(.touchDown) { control, event in
// UIControlEvents.touchDown event code
}
这两个UIImagePickerController代码片段是等价的。如您所见,有多种方法可以通过混合和匹配各种便利方法和闭包处理程序提供更细粒度的控制。
UIImagePickerController(source: .camera, allow: .image) { result, picker in
myImageView.image = result.editedImage
}.present(from: self)
let pickerController = UIImagePickerController()
pickerController.sourceType = .camera
pickerController.mediaTypes = [kUTTypeImage]
pickerController.didFinishPickingMedia { [weak self] info in
myImageView.image = info[UIImagePickerControllerEditedImage] as? UIImage
self?.dismiss(animated: true)
}.didCancel { [weak self] in
self?.dismiss(animated: true)
}
self.present(pickerController, animated: true)
深入理解
根据您的学习风格,有多种方法可以了解更多关于闭包
API 的信息。有些人喜欢打开Xcode并使用自动完成来查看各种属性/函数。其他人更喜欢更详细的方法。以下是一些文档选项。
游乐场
要尝试游乐场演示,请打开Closures
工作空间(Closures.xcworkspace文件),构建Closures
框架目标,然后点击ClosuresDemo
游乐场,点击播放按钮。
类参考文档
参考文档 包含了所有详细的使用信息,包括所有公开的方法、参数和方便的初始化器。
安装
Swift 包管理器
如果使用 Swift 包管理器,在 Xcode 中,转到 文件 > Swift 包 > 添加包依赖...
并输入以下 URL:
https://github.com/vhesener/Closures
CocoaPods
如果使用 CocoaPods,请将以下内容添加到您的 Podfile:
pod 'Closures'
Carthage
如果使用 Carthage,请将以下内容添加到您的 Cartfile:
github "vhesener/Closures"
手动方式
下载或克隆位于 主分支 中的项目文件。将位于 'Closures/Source' 子目录中的所有 .swift 文件拖放到您的 Xcode 项目中。选择 需要时复制项目 选项。
背景
受 BlocksKit 的启发,需要有一个更加 Swift式 的相同库版本。这个库的目标是提供相似的功能,但以下是一些限制
- 在 API 中尽可能多地使用 Swift 的强类型系统。
- 不使用 Objective-C 运行时。这样做有多个原因,但主要因为
- 这非常有挑战性。
- 这与 Swift 的精神相符。
- 创建一种可扩展的机制,以便未来轻松添加额外的闭包包装器。
我们的目标是通过 sherlock 来变得无关紧要。除了不再需要支持这个库之外,实际上是由苹果的 API 团队验证也令人感到骄傲。
想了解更多?
如果您希望看到一个使用闭包转换的 API,却一无所获,那么所有的一切都可以得到补偿。只需通过添加
许可
Closures 在 MIT 许可证 下提供。
The MIT License (MIT)
Copyright (c) 2017 Vincent Hesener
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.