Resource
Resource帮助您管理项目资源。
注意:Resource仍在开发中,很多功能都可能发生变更。
特性
- 面向协议的设计
- 全面的单元测试覆盖
- 完整的文档
需求
iOS 10+ / Xcode 9+ / Swift 4+
安装
Carthage
Carthage 是一个去中心化的依赖管理器。要安装 Resource,请将以下行添加到您的 Cartfile
github "TintPoint/Resource" ~> 0.3
CocoaPods
CocoaPods 是一个中心化的依赖管理器。要安装 Resource,请将以下行添加到您的 Podfile
pod 'Resource', '~> 0.3'
入门指南
使用后缀为 Describing
的协议定义一个自定义枚举(可用协议列表请见 此处)。例如,要管理警告控制器,请编写以下代码。
enum Alert: AlertControllerDescribing {
case databaseError, networkError
var title: String {
switch self {
case .databaseError: return "Database Error"
case .networkError: return "Network Error"
}
}
var message: String {
switch self {
case .databaseError: return "Please try again."
case .networkError: return "Please check your network connection."
}
}
var actions: [UIAlertAction] {
return [UIAlertAction(title: "OK", style: .default)]
}
}
let alert = Resource.of(Alert.databaseError)
present(alert, animated: true)
为视图控制器定义泛型方法
定义一个遵循 CustomViewController
协议的 UIViewController
子类。
class AppController: UIViewController, CustomViewController {
static let representedBy: ViewControllerDescribing = ViewControllerDescription(name: "Main", storyboard: UIStoryboard(name: "Main", bundle: Bundle.main))
}
let controller = Resource.of(AppController.self)
print(type(of: controller)) // AppController
类型安全的依赖注入也得到了支持。
class AppController: UIViewController, DataReceivingController {
static let representedBy: ViewControllerDescribing = ViewControllerDescription(name: "Main", storyboard: UIStoryboard(name: "Main", bundle: Bundle.main))
var controllerData: (text: String, number: Int)?
}
let controller = Resource.of(AppController.self, passing: (text: "Text", number: 10))
print(controller.controllerData?.text) // "Text"
print(controller.controllerData?.number) // 10
参考
可用协议
- AlertActionDescribing
- AlertControllerDescribing
- LocalizedStringDescribing
- LocalizedUserNotificationStringDescribing
- StoryboardDescribing
- StringDescribing
- ViewControllerDescribing
可用的结构体
- AlertActionDescription
- AlertControllerDescription
- LocalizedStringDescription
- LocalizedUserNotificationStringDescription
- StoryboardDescription
- StringDescription
- ViewControllerDescription