将 class 成员的引用分配给 Swift 中的变量
Assigning a reference from a class member to variable in Swift
class UserDataStore {
}
class ProfileInteractor {
var userDataStore: UserDataStore?
init(userDataStore: UserDataStore?) {
self.userDataStore = userDataStore
}
}
var profileInteractor = ProfileInteractor(userDataStore: UserDataStore())
var userDataStore = profileInteractor.userDataStore
profileInteractor.userDataStore = nil
print(profileInteractor.userDataStore == nil) // prints: true
print(userDataStore == nil) // prints: false
print(userDataStore === profileInteractor.userDataStore) // prints: false
- 谁能解释一下,为什么 userDataStore 不指向 profileInteractor.userDataStore !?
- userDataStore 指向它,我必须做什么?
复制引用,隐式创建共享实例。复制后,两个变量再引用数据的单个实例,因此修改第二个变量中的数据也会影响原始
class UserDataStore {
var inst:String?
init(obj: String?) {
self.inst = obj
}
}
class ProfileInteractor {
var userDataStore: UserDataStore?
init(userDataStore: UserDataStore?) {
self.userDataStore = userDataStore
}
}
var profileInteractor = ProfileInteractor(userDataStore: UserDataStore(obj: "10"))
var userDataStore = profileInteractor.userDataStore
withUnsafePointer(to: &(profileInteractor.userDataStore)) {
print(" profileInteractor.userDataStore has address: \([=10=])")
}
withUnsafePointer(to: &userDataStore) {
print("userDataStore has address: \([=10=])")
}
withUnsafePointer(to: &(((profileInteractor.userDataStore) as! UserDataStore).inst)) {
print(" profileInteractor.userDataStore value inst has address: \([=10=])")
}
withUnsafePointer(to: &(((userDataStore) as! UserDataStore).inst)) {
print("userDataStore value inst has address: \([=10=])")
}
print(userDataStore)
print(profileInteractor.userDataStore)
profileInteractor.userDataStore = nil
print(profileInteractor.userDataStore == nil) // prints: true
print(userDataStore == nil) // prints: false
print(userDataStore === profileInteractor.userDataStore) // prints: false
输出:
profileInteractor.userDataStore has address: 0x0000610000031230
userDataStore has address: 0x000000010e8103e0
profileInteractor.userDataStore value inst has address: 0x000061000004f5b0
userDataStore value inst has address: 0x000061000004f5b0
Optional(__lldb_expr_35.UserDataStore)
Optional(__lldb_expr_35.UserDataStore)
true
false
false
这是按值传递和按引用传递的很好的参考:
https://developer.apple.com/swift/blog/?id=10
why userDataStore doesn't point to profileInteractor.userDataStore !?
这一行:
var profileInteractor = ProfileInteractor(userDataStore: UserDataStore())
您创建了一个新的 ProfileInteractor
对象和一个新的 UserDataStore
对象。然后,您使 profileInteractor
引用 ProfileInteractor
对象。
下一行:
var userDataStore = profileInteractor.userDataStore
您正在使 userDataStore
与 profileInteractor.userDataStore
引用同一对象。现在,您使用 userDataStore
对 UserDataStore
对象所做的任何操作都将反映在 profileInteractor.userDataStore
上,因为它们本质上是相同的。
然而,这一行改变了一切:
profileInteractor.userDataStore = nil
你让 profileInteractor.userDataStore
没有任何意义。这 不 意味着 UserDataStore
对象被销毁了。这只是意味着您不能再使用 profileInteractor.userDataStore
访问 UserDataStore
对象。但是猜猜什么仍然指的是 UserDataStore
对象? userDataStore
!
所以你的问题的答案是 userDataStore
确实指向与 profileInteractor.userDataStore
相同的对象,直到你将 profileInteractor.userDataStore
设置为 nil。
What must I do, that userDataStore points to it?
嗯,你已经做到了。您刚刚在下一行代码中使 profileInteractor.userDataStore
指向 其他内容。
如果您希望一个局部变量始终指向另一个局部变量指向的同一事物,您的代码可能会变得非常混乱,就像 Ankit Thakur 的回答中那样,有不安全的指针和东西。如果要将变量传递给方法,则可以使用 inout
关键字。
我认为你误解了将nil
分配给userDataStore
的目的。
将userDataStore
赋值给nil
意味着它不会指向任何东西,即它会被销毁;那应该不导致让profileInteractor.userDataStore
也nil
。 Swift中的nil
是什么都没有的意思,not
是指向内存中一个空的space的指针。
为了更清楚,请检查以下代码片段:
class UserDataStore: CustomStringConvertible {
var title: String?
var description: String {
return "\(title)"
}
init(_ title: String) {
self.title = title
}
}
class ProfileInteractor {
var userDataStore: UserDataStore?
init(userDataStore: UserDataStore?) {
self.userDataStore = userDataStore
}
}
var profileInteractor = ProfileInteractor(userDataStore: UserDataStore("Hello"))
var userDataStore = profileInteractor.userDataStore
print(userDataStore === profileInteractor.userDataStore) // prints: true
print(profileInteractor.userDataStore) // prints: Optional(Optional("Hello"))
userDataStore?.title = "Bye"
print(profileInteractor.userDataStore) // prints: Optional(Optional("Bye"))
userDataStore = nil
print(profileInteractor.userDataStore) // prints: Optional(Optional("Bye"))
希望对您有所帮助。
class UserDataStore {
}
class ProfileInteractor {
var userDataStore: UserDataStore?
init(userDataStore: UserDataStore?) {
self.userDataStore = userDataStore
}
}
var profileInteractor = ProfileInteractor(userDataStore: UserDataStore())
var userDataStore = profileInteractor.userDataStore
profileInteractor.userDataStore = nil
print(profileInteractor.userDataStore == nil) // prints: true
print(userDataStore == nil) // prints: false
print(userDataStore === profileInteractor.userDataStore) // prints: false
- 谁能解释一下,为什么 userDataStore 不指向 profileInteractor.userDataStore !?
- userDataStore 指向它,我必须做什么?
复制引用,隐式创建共享实例。复制后,两个变量再引用数据的单个实例,因此修改第二个变量中的数据也会影响原始
class UserDataStore {
var inst:String?
init(obj: String?) {
self.inst = obj
}
}
class ProfileInteractor {
var userDataStore: UserDataStore?
init(userDataStore: UserDataStore?) {
self.userDataStore = userDataStore
}
}
var profileInteractor = ProfileInteractor(userDataStore: UserDataStore(obj: "10"))
var userDataStore = profileInteractor.userDataStore
withUnsafePointer(to: &(profileInteractor.userDataStore)) {
print(" profileInteractor.userDataStore has address: \([=10=])")
}
withUnsafePointer(to: &userDataStore) {
print("userDataStore has address: \([=10=])")
}
withUnsafePointer(to: &(((profileInteractor.userDataStore) as! UserDataStore).inst)) {
print(" profileInteractor.userDataStore value inst has address: \([=10=])")
}
withUnsafePointer(to: &(((userDataStore) as! UserDataStore).inst)) {
print("userDataStore value inst has address: \([=10=])")
}
print(userDataStore)
print(profileInteractor.userDataStore)
profileInteractor.userDataStore = nil
print(profileInteractor.userDataStore == nil) // prints: true
print(userDataStore == nil) // prints: false
print(userDataStore === profileInteractor.userDataStore) // prints: false
输出:
profileInteractor.userDataStore has address: 0x0000610000031230
userDataStore has address: 0x000000010e8103e0
profileInteractor.userDataStore value inst has address: 0x000061000004f5b0
userDataStore value inst has address: 0x000061000004f5b0
Optional(__lldb_expr_35.UserDataStore)
Optional(__lldb_expr_35.UserDataStore)
true
false
false
这是按值传递和按引用传递的很好的参考: https://developer.apple.com/swift/blog/?id=10
why userDataStore doesn't point to profileInteractor.userDataStore !?
这一行:
var profileInteractor = ProfileInteractor(userDataStore: UserDataStore())
您创建了一个新的 ProfileInteractor
对象和一个新的 UserDataStore
对象。然后,您使 profileInteractor
引用 ProfileInteractor
对象。
下一行:
var userDataStore = profileInteractor.userDataStore
您正在使 userDataStore
与 profileInteractor.userDataStore
引用同一对象。现在,您使用 userDataStore
对 UserDataStore
对象所做的任何操作都将反映在 profileInteractor.userDataStore
上,因为它们本质上是相同的。
然而,这一行改变了一切:
profileInteractor.userDataStore = nil
你让 profileInteractor.userDataStore
没有任何意义。这 不 意味着 UserDataStore
对象被销毁了。这只是意味着您不能再使用 profileInteractor.userDataStore
访问 UserDataStore
对象。但是猜猜什么仍然指的是 UserDataStore
对象? userDataStore
!
所以你的问题的答案是 userDataStore
确实指向与 profileInteractor.userDataStore
相同的对象,直到你将 profileInteractor.userDataStore
设置为 nil。
What must I do, that userDataStore points to it?
嗯,你已经做到了。您刚刚在下一行代码中使 profileInteractor.userDataStore
指向 其他内容。
如果您希望一个局部变量始终指向另一个局部变量指向的同一事物,您的代码可能会变得非常混乱,就像 Ankit Thakur 的回答中那样,有不安全的指针和东西。如果要将变量传递给方法,则可以使用 inout
关键字。
我认为你误解了将nil
分配给userDataStore
的目的。
将userDataStore
赋值给nil
意味着它不会指向任何东西,即它会被销毁;那应该不导致让profileInteractor.userDataStore
也nil
。 Swift中的nil
是什么都没有的意思,not
是指向内存中一个空的space的指针。
为了更清楚,请检查以下代码片段:
class UserDataStore: CustomStringConvertible {
var title: String?
var description: String {
return "\(title)"
}
init(_ title: String) {
self.title = title
}
}
class ProfileInteractor {
var userDataStore: UserDataStore?
init(userDataStore: UserDataStore?) {
self.userDataStore = userDataStore
}
}
var profileInteractor = ProfileInteractor(userDataStore: UserDataStore("Hello"))
var userDataStore = profileInteractor.userDataStore
print(userDataStore === profileInteractor.userDataStore) // prints: true
print(profileInteractor.userDataStore) // prints: Optional(Optional("Hello"))
userDataStore?.title = "Bye"
print(profileInteractor.userDataStore) // prints: Optional(Optional("Bye"))
userDataStore = nil
print(profileInteractor.userDataStore) // prints: Optional(Optional("Bye"))
希望对您有所帮助。