Can someone explain when and when not to use a 'weak' assignment to a delegate pointer in Swift, and why?
My understanding is that if you use a protocol that is not defined as a class you cannot, nor want to, assign your delegate pointer to weak.
protocol MyStructProtocol{
//whatever
}
struct MyStruct {
var delegate: MyStructProtocol?
}
However, when your protocol is defined as a class type protocol then you DO want to set your delegate to a weak pointer?
protocol MyClassProtocol: class{
//whatever
}
class MyClass {
weak var delegate: MyClassProtocol?
}
Am I correct? In Apple's swift guide there class protocol examples aren't using weak assignments, but in my testing I'm seeing strong reference cycles if my delegates aren't weakly referenced.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…