ToolBox
ToolBox 是一个 Swift 编写的工具箱库 ;)
安装
ToolBox 可以通过CocoaPods 来使用。要安装它,只需将以下行添加到您的 Podfile 中
pod 'ToolBox'
路由器
路由器可以帮助您管理应用程序中的路由和导航。
路由是一个实现了 TBRoutable 协议的类(通常是 UIViewController
)
public protocol TBRoutable: class {
static func loadController(with data: Any?, for route: String) -> UIViewController?
}
TBRoutable 协议有一个默认实现,仅仅返回指定的初始化器。如果需要更多控制初始化过程,您可以重写它。
class HomeViewController: UIViewController, TBRoutable {
public static func loadController(with data: Any?, for route: String) -> UIViewController? {
//You can add some logic like data validation
return self.init()
}
}
可以通过手动方式添加路由
extension TBRouter.Route {
static let home = TBRouter.Route("home")
}
TBRouter.addRoute(.home, routableClass: HomeViewController.self)
或者从一个本地或远程的路由文件(JSON):Routes.json
{
"home": {
"class" : "HomeViewController"
},
}
if let url = Bundle.main.url(forResource: "Routes", withExtension: "json") {
TBRouter.loadRoutes(from: url)
}
要执行一个路由,只需从 UIViewController
中调用 x_performRoute
x_performRoute(.home, presentationType: .push)
//or
x_performRoute(.home, presentationType: .modal, data: someData, animated: true)
或者直接获取路由的控制器并编程进行显示
let homeController = TBRouter.route(.home, with: someData)
容器
TBContainer
是一个键/值 RAM 存储空间
//Set
TBContainer.add(1, for: "key")
//Get
let val: Int? = TBContainer.getValue(for: "key")
//Remove
val = TBContainer.removeValue(for: "key")
键与值类型相关联,因此可以使用相同的键进行重复使用
TBContainer.add(1, for: "key")
TBContainer.add("Hello", for: "key")
let val: Int? = TBContainer.getValue(for: "key") // 1
let val: String? = TBContainer.getValue(for: "key") // "Hello"
请注意,TBContainer
不与继承一起工作,set
和 get
需要使用相同的类型
TBContainer.add(myCollectionView, for: "collectionView")
let view: UIView? = TBContainer.getValue(for: "collectionView") // nil
let collectionView: UICollectionView? = TBContainer.getValue(for: "collectionView") // myCollectionView
服务
TBServices
是一个服务容器。要创建一个服务并将其提供
- 创建继承自
TBServiceProtocol
的MyServiceProtocol
协议 - 创建你的服务,实现前面的协议和
TBServiceProtocol
- 将服务添加到
TBServices
protocol MyServiceProtocol: TBServiceProtocol {
// Add methods
}
final class MyService: MyServiceProtocol {
// Add protocol implementation
static func loadService() -> MyService {
return MyService() // Service can be a singleton or not
}
}
// Add service
TBServices.add(MyService.self, for: MyServiceProtocol.self)
// Retrieve service
let service: MyServiceProtocol = TBServices.retrieve()
// or
TBServices.retrieve(type: MyServiceProtocol.self)
请注意:服务需要在检索之前添加
功能切换
你可以创建一个新的功能定义
extension TBFeature.Name {
public static let foo = TBFeature.Name("foo")
public static let bar = TBFeature.Name("bar")
}
并设置/获取功能状态。
// Set
TBFeature.set(.foo, enabled: true)
TBFeature.set(.foo, enabled: false)
// Get
TBFeature.isEnabled(.foo) // false
功能状态改变时会发送通知(tbFeaturesDidChange
)
NotificationCenter.default.addObserver(forName: .tbFeaturesDidChange, object: nil, queue: .main) { (notif) in
if TBFeature.feature(.foo, matchNotification: notif) {
// Foo feature status did change
}
}
打印机
TBPrint
允许你打印信息并切换日志记录。只有在开启 DEBUG 标志时,记录器才会运行。
tbPrint("message", category: .ui)
tbDebugPrint("message", category: .ui)
要创建新的类别,只需添加
extension TBPrint.Category {
static let foo = TBPrint.Category("foo")
}
tbPrint("message", category: .foo)
默认类别包括
网络
数据库
数据
文件
服务
UI
无
全部
你可以选择哪些类别要记录
TBPrint.categories = [PrintCategory.ui, PrintCategory.file]
//or
TBPrint.categories = [PrintCategory.all]
TBReusable
TBReusable 是一个针对单元格(UICollectionView
和 UITableViewCell
)的协议,它简化了单元格的注册和检索。
public class MyTableViewCell: UITableViewCell, TBReusable {
}
// Register
tableView.x_registerReusableCell(MyTableViewCell.self)
// Dequeue
let cell: MyTableViewCell = tableView.x_dequeueReusableCell(indexPath: indexPath)
其它
ToolBox 包含其他未记录的功能,例如
- 具有自定义标签栏项的标签栏
- 具有自定义转场的翻页器
- 一些扩展(
Date
、Array
、Float
、String
、AVURLAsset
、UIViewController
...) UI Object
需求
- iOS 10.0+ / tvOS 10.0+
- Xcode 9.4+
- Swift 4.2+
示例
要运行示例项目,请首先克隆仓库,然后从“示例”目录运行pod install
。
作者
Nicolas Berthelot
许可
ToolBox遵循MIT许可。有关更多信息,请参阅LICENSE文件。