我試圖讓我的頭繞過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