为什么在这种情况下 isUniquelyReferencedNonObjC returns false?
Why isUniquelyReferencedNonObjC returns false in this case?
我正在研究 Swift 中引用计数的工作原理。在下面的代码片段中,我实例化了一个全新的 Person 对象并检查它是否被唯一引用。我相信它是唯一引用的,因为它只保留在 "person" 实例上。但是,isUniquelyReferencedNonObjC 函数 return 为假。
谁能解释这是为什么?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
var person = Person()
// this will output not unique
if isUniquelyReferencedNonObjC(&person) {
println("unique")
} else {
println("not unique")
}
return true
}
人 class:
class Person: NSObject {
}
EDIT 有趣的是,当我不让 Person 成为 NSObject 的子 class 时,isUniquelyReferencedNonObjC 将按预期 return 为真。但是,我仍然不明白为什么 subclassing NSObjct 会在这里有所作为。
提前致谢。
isUniquelyReferencedNonObjC
记录为
/// Returns `true` iff `object` is a non-\ `@objc` class instance with
/// a single strong reference.
/// ...
/// * If `object` is an Objective-C class instance, returns `false`.
/// ...
您的 Person
class 继承自 NSObject
,因此
isUniquelyReferencedNonObjC(&person)
returns false
.
我正在研究 Swift 中引用计数的工作原理。在下面的代码片段中,我实例化了一个全新的 Person 对象并检查它是否被唯一引用。我相信它是唯一引用的,因为它只保留在 "person" 实例上。但是,isUniquelyReferencedNonObjC 函数 return 为假。
谁能解释这是为什么?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
var person = Person()
// this will output not unique
if isUniquelyReferencedNonObjC(&person) {
println("unique")
} else {
println("not unique")
}
return true
}
人 class:
class Person: NSObject {
}
EDIT 有趣的是,当我不让 Person 成为 NSObject 的子 class 时,isUniquelyReferencedNonObjC 将按预期 return 为真。但是,我仍然不明白为什么 subclassing NSObjct 会在这里有所作为。
提前致谢。
isUniquelyReferencedNonObjC
记录为
/// Returns `true` iff `object` is a non-\ `@objc` class instance with
/// a single strong reference.
/// ...
/// * If `object` is an Objective-C class instance, returns `false`.
/// ...
您的 Person
class 继承自 NSObject
,因此
isUniquelyReferencedNonObjC(&person)
returns false
.