2
这里是我的代码:迅捷授权 - 何时使用弱引用,为什么'委托'是零?
protocol Delegate: NSObjectProtocol {}
class A: NSObject {
weak var delegate: Delegate!
override init() {
super.init()
}
func start() {
//synchronous------- print A: false
print("A:", delegate == nil)
//asynchronous------- print B: true Why is 'true'? How can do that not be released?
let time: NSTimeInterval = 1
let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(time * Double(NSEC_PER_SEC)))
dispatch_after(delay, dispatch_get_main_queue()) {
print("B:", self.delegate == nil)
}
}
}
class B: NSObject, Delegate {
override init() {
super.init()
let a = A()
a.delegate = self
a.start()
}
}
我搜索了关于堆栈溢出的其他问题,但我无法找到的东西,可以帮助我充分理解这种情况。
看看这个:http://stackoverflow.com/questions/34608397/why-is-the-uitabbarcontrollerdelegate-nil-right-after-i-set-it/34608887,也许这是一个类似的情况。 – courteouselk