是的,是的。不管字符串和Int是
在里面
一个班。你要求字符串或者Int(或者两者都有),这些都是值类型,你得到了副本。
这很容易向你自己证明,尤其是用绳子。只需改变它的一些内容,然后再回头看看类实例所保存的内容:它将保持不变。
class C {
var stringProperty : String
init(string:String) {
self.stringProperty = string
}
}
let c = C(string:"hello")
var s = c.stringProperty
s.removeLast()
print(s) // hell
print(c.stringProperty) // hello
那些
class C {
var stringProperty : String
init(string:String) {
self.stringProperty = string
}
}
let c = C(string:"hello")
let d = c
c.stringProperty = "goodbye"
print(d.stringProperty) // goodbye