如何使用台风在 swift 中注入委托?
how to inject a delegate in swift using typhoon?
使用台风我试图在我的视图控制器中注入 "worker" 的类型。我的 "Worker" 需要一个委托,以便在工作完成时调用此方法。我需要将我的视图控制器设置为注入的工作人员 class 的委托。换句话说,循环依赖。
更新问题来源:
//my typhoon assembly class
import Typhoon
class Assembly : TyphoonAssembly {
public dynamic func viewController() -> AnyObject {
return TyphoonDefinition.withClass(ViewController.self) {
(definition) in
definition.injectProperty("worker", with: self.foo())
definition.scope = TyphoonScope.Singleton
}
}
public dynamic func foo() -> AnyObject {
return TyphoonDefinition.withClass(Foo.self) {
(definition) in
definition.injectProperty("delegate", with: self.viewController())
}
}
}
Foo
是完成工作的地方,它实现了 WorkHandler
协议并且有一个 SomeProtocol
类型的委托在工作完成时调用:
import Foundation
@objc
protocol SomeProtocol: class {
optional func hasFinishedWork(value: Bool)
}
protocol WorkHandler : class {
func doSomething()
}
class Foo: WorkHandler{
//how will this get set?
var delegate:SomeProtocol?
func doSomething(){
print("doing the work")
delegate?.hasFinishedWork!(true)
}
}
我的 ViewController 像这样符合 SomeProtocol:
import UIKit
class ViewController: UIViewController, SomeProtocol{
var worker:WorkHandler?
override func viewDidLoad() {
super.viewDidLoad()
worker?.doSomething();
}
@objc func hasFinishedWork(value: Bool){
print("The work was all done")
}
}
以上代码在运行时出现以下错误:
2016-02-29 20:25:43.250 TestApp[30604:5316415] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Subclass of NSProxy or NSObject is required.'
有人能帮忙吗?
原来我必须让我的协议继承自 NSObject:
@objc
protocol SomeProtocol: class {
optional func hasFinishedWork(value: Bool)
}
@objc
protocol WorkHandler : class {
func doSomething()
}
class Foo: NSObject, WorkHandler{
//how will this get set?
var delegate:SomeProtocol?
@objc func doSomething(){
print("doing the work")
delegate?.hasFinishedWork!(true)
}
}
现在它按预期工作了。
使用台风我试图在我的视图控制器中注入 "worker" 的类型。我的 "Worker" 需要一个委托,以便在工作完成时调用此方法。我需要将我的视图控制器设置为注入的工作人员 class 的委托。换句话说,循环依赖。
更新问题来源:
//my typhoon assembly class
import Typhoon
class Assembly : TyphoonAssembly {
public dynamic func viewController() -> AnyObject {
return TyphoonDefinition.withClass(ViewController.self) {
(definition) in
definition.injectProperty("worker", with: self.foo())
definition.scope = TyphoonScope.Singleton
}
}
public dynamic func foo() -> AnyObject {
return TyphoonDefinition.withClass(Foo.self) {
(definition) in
definition.injectProperty("delegate", with: self.viewController())
}
}
}
Foo
是完成工作的地方,它实现了 WorkHandler
协议并且有一个 SomeProtocol
类型的委托在工作完成时调用:
import Foundation
@objc
protocol SomeProtocol: class {
optional func hasFinishedWork(value: Bool)
}
protocol WorkHandler : class {
func doSomething()
}
class Foo: WorkHandler{
//how will this get set?
var delegate:SomeProtocol?
func doSomething(){
print("doing the work")
delegate?.hasFinishedWork!(true)
}
}
我的 ViewController 像这样符合 SomeProtocol:
import UIKit
class ViewController: UIViewController, SomeProtocol{
var worker:WorkHandler?
override func viewDidLoad() {
super.viewDidLoad()
worker?.doSomething();
}
@objc func hasFinishedWork(value: Bool){
print("The work was all done")
}
}
以上代码在运行时出现以下错误:
2016-02-29 20:25:43.250 TestApp[30604:5316415] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Subclass of NSProxy or NSObject is required.'
有人能帮忙吗?
原来我必须让我的协议继承自 NSObject:
@objc
protocol SomeProtocol: class {
optional func hasFinishedWork(value: Bool)
}
@objc
protocol WorkHandler : class {
func doSomething()
}
class Foo: NSObject, WorkHandler{
//how will this get set?
var delegate:SomeProtocol?
@objc func doSomething(){
print("doing the work")
delegate?.hasFinishedWork!(true)
}
}
现在它按预期工作了。