我试图让我的头绕过Swift代表,并且偷走了一个Playground,但似乎无法获得要调用的委托函数。Swift 3没有调用的代理函数
protocol fBookDelegate:class {
func processData(data: String)
}
class fBook {
weak var delegate: fBookDelegate?
init() {
print("initialising fBook")
delegate?.processData(data: "hello world")
print("we should have printed")
}
}
class fMain: fBookDelegate {
init() {
print("initialising fMain")
let getfBook = fBook()
getfBook.delegate = self
print("all done let's rumble")
}
func processData(data: String) {
print("processing data from fBook with \(data)")
}
}
var controller = fMain()
任何人都可以发现我的错误吗?
我得到的输出是
initialising fMain
initialising fBook
we should have printed
all done let's rumble
你试图使用委托在fBook的初始化,但你不设置它,直到你初始化后目的。 –
修改fBook init以将委托作为参数,而不是将其指定为属性。 – ghostatron