In the following code NSString
and NSNumber
are not de-initialised when the reference is removed . NSMutableString
and NSAttributedString
are de-initialised . What is the criteria for deinit ?
class WeakHolder<R : AnyObject> {
weak var cheez : R?
init(_ _cheez : R) {
cheez = _cheez
}
}
do {
var nsStringCollection = [NSString(string: "77"),NSString(string: "99")]
let weakNSStringHolder = WeakHolder(nsStringCollection[1])
nsStringCollection.removeLast()
print("NSString : \(weakNSStringHolder.cheez)")
}
do {
var nsMutableStringCollection = [NSMutableString(string: "77_m"),NSMutableString(string: "99_m")]
let weakNSMutableStringHolder = WeakHolder(nsMutableStringCollection[1])
nsMutableStringCollection.removeLast()
print("NSMutableString : \(weakNSMutableStringHolder.cheez)")
}
do {
var nsNumberCollection = [NSNumber(integerLiteral: 77),NSNumber(integerLiteral: 99)]
let weakNumberHolder = WeakHolder(nsNumberCollection[1])
nsNumberCollection.removeLast()
print("Number : \(weakNumberHolder.cheez)")
}
do {
var nsAttributedCollection = [NSAttributedString(string: "77_atts"),NSAttributedString(string: "99_atts")]
let weakAttributedHolder = WeakHolder(nsAttributedCollection[1])
nsAttributedCollection.removeLast()
print("AttrString : \(weakAttributedHolder.cheez)")
}
Output :
NSString : Optional(99)
NSMutableString : nil
Number : Optional(99)
AttrString : nil