在Swift中将类成员的引用分配给变量

时间:2017-05-28 10:01:12

标签: swift class reference var

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指向它?

3 个答案:

答案 0 :(得分:2)

我认为您误解nil分配给userDataStore的目的。

userDataStore分配给nil意味着它不会指向任何东西,即它将被销毁;这应该导致让profileInteractor.userDataStore也是nil。 Swift中的nil意味着什么都没有,not意味着它是指向内存中空白空间的指针。

为了更清楚,请查看以下代码段:

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"))

希望这会有所帮助。

答案 1 :(得分:1)

  

为什么userDataStore没有指向profileInteractor.userDataStore!?

在这一行:

var profileInteractor = ProfileInteractor(userDataStore: UserDataStore())

您创建了一个新的ProfileInteractor对象和一个新的UserDataStore对象。然后,您使profileInteractor引用ProfileInteractor对象。

在下一行:

var userDataStore = profileInteractor.userDataStore

您正在使userDataStore引用与profileInteractor.userDataStore相同的对象。现在,使用UserDataStoreuserDataStore}对象所做的任何操作都会反映在profileInteractor.userDataStore上,因为它们基本上是相同的。

然而,这一行改变了一切:

profileInteractor.userDataStore = nil

你让profileInteractor.userDataStore没有提及。 意味着UserDataStore对象被销毁。这只是意味着您不再可以使用profileInteractor.userDataStore来访问UserDataStore对象。但是猜猜仍然指的是UserDataStore对象? userDataStore

所以问题的答案是userDataStore确实指向与profileInteractor.userDataStore相同的对象,直到您将profileInteractor.userDataStore设置为nil。

  

我该怎么办,userDataStore指向它?

嗯,你已经做到了。您刚刚在下一行代码中将profileInteractor.userDataStore指向其他

如果你想让局部变量始终指向另一个局部变量所指向的同一个东西,那么你的代码就会变得非常混乱,就像在Ankit Thakur的答案中一样,有不安全的指针和东西。如果要将变量传递给方法,则可以使用inout关键字。

答案 2 :(得分:0)

复制引用,隐式创建共享实例。复制后,两个变量然后引用单个数据实例,因此修改第二个变量中的数据也会影响原始

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: \($0)")
}
withUnsafePointer(to: &userDataStore) {
    print("userDataStore has address: \($0)")
}

withUnsafePointer(to: &(((profileInteractor.userDataStore) as! UserDataStore).inst)) {
    print(" profileInteractor.userDataStore value inst has address: \($0)")
}
withUnsafePointer(to: &(((userDataStore) as! UserDataStore).inst)) {
    print("userDataStore value inst has address: \($0)")
}

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

相关问题