在以下代码中NSString
,并且NSNumber
在删除引用时不会取消初始化。NSMutableString
并且NSAttributedString
被取消初始化。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)")
}
输出 :
NSString : Optional(99)
NSMutableString : nil
Number : Optional(99)
AttrString : nil