【发布时间】:2017-09-27 17:19:15
【问题描述】:
我正试图了解 Swift 委托并偷走/敲掉 Playground,但似乎无法调用委托函数。
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 的 init 中使用委托,但直到您初始化对象后才设置它。
-
修改 fBook init 以将委托作为参数而不是之后将其分配为属性。
标签: ios xcode swift3 delegates swift-playground