TargetBox
分裂文件在独立部分上具有两个或更多目标。
关于视图控制器的示例和解释
首先您需要定义并继承自String和 Targets Type一个新的 Targets 枚举
enum ExampleTargets: String, TargetsType {
// String version should be the same as target name. It's case sensitive.
case targetBox = "TargetBox"
case anotherTarget = "AnotherTarget"
}
然后为每个目标定义函数,并在控制器类中添加 box 变量
private var box: TargetsBox<TargetBoxEmulation, ExampleTargets>?
private func prepareTargetBox() {
print("Hello Box Target")
}
private func prepareForOthers() {
print("Hello Others Target")
}
然后定义如下函数并在 init 中调用它
private func createTargetBox() {
// This will create weak reference on function in your controller.
let targetBoxFunction = WeakFunction(
signature: ExampleViewController.prepareTargetBox,
object: self
)
/*
@param function Created early weak function.
@param trigger Selector after which our weaked function will be called, function must be dynamic or marked as dynamic
@param target The target, what be defined early, on which the function should be called
*/
let containerTargetBox = TargetingFunctionContainer(
function: targetBoxFunction,
trigger: #selector(ExampleViewController.viewDidLoad),
target: ExampleTargets.targetBox
)
let otherTargetFunction = WeakFunction(
signature: ExampleViewController.prepareForOthers,
object: self
)
let containerOthers = TargetingFunctionContainer(
function: otherTargetFunction,
trigger: #selector(ExampleViewController.viewDidLoad),
target: ExampleTargets.anotherTarget
)
self.box = TargetsBox(containers: [containerTargetBox, containerOthers])
}
然后当某人调用您的触发函数时,将调用 selected target 上的 weak function。
安装
TargetBox可以通过CocoaPods获得。要安装它,只需将以下行添加到Podfile中
pod 'TargetBox'
作者
Kikacheishvili Bogdan, [email protected]
许可证
TargetBox 可在 MIT 许可证下获得。更多信息请参阅 LICENSE 文件。