我在通过 segue 传递数据时发现了一些奇怪的东西,我只是不明白为什么会这样。我知道我很可能对类(class)的性质有一些误解,所以如果我能获得一些关于它的知识的帮助,我将不胜感激。
当我通过 segue(都连接到导航 Controller )将 Int 或 String 传递给第二个 View 时,然后在第二个 View 中更改该值,然后返回到第一个,该 Int 的值或字符串将与最初在第一个 View 中设置的相同,不保留在第二个 View 中更改的值。这是有道理的,也是我所期望的。
但我注意到在使用自定义类时情况并非如此。如果我更改了属于该类的值,它们会在返回第一个 View 时保留。我确实注意到,如果我尝试用一个全新的类替换整个类,那将不会发生,这类似于尝试更改整个 Int,但为什么在这种情况下仅更改类的属性仍然有效?
我确实知道如何将数据传回之前的 View ,这样我可以更好地理解为什么事情会这样工作。
我一直在玩它,这里的代码可以更好地解释我在说什么:
第一个 View Controller :
class ViewController: UIViewController {
var number = 5
var string = "Hello from View 1"
var object = Object(number: 5, string: "Hello from View 1", bool: false)
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("AGE 1 NUMBER: ", number)
print("AGE 1 STRING: ", string)
print("AGE 1 OBJECT NUMBER: ", object.number)
print("AGE 1 OBJECT STRING: ", object.string)
print("AGE 1 OBJECT BOOL: ", object.bool)
}
@IBAction func buttonPress(_ sender: Any) {
self.performSegue(withIdentifier: "toSecond", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toSecond" {
let vc = segue.destination as! SecondViewController
vc.number = number
vc.string = string
vc.object = object
}
}
}
第二个 View Controller :
class SecondViewController: UIViewController {
var number = 0
var string = ""
var object = Object()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("AGE 2 NUMBER: ", number)
print("AGE 2 STRING: ", string)
print("AGE 2 OBJECT NUMBER: ", object.number)
print("AGE 2 OBJECT STRING: ", object.string)
print("AGE 2 OBJECT BOOL: ", object.bool)
number = 12
string = "Hello back from View 2"
object.number = 12
object.string = "Hello back from View 2"
object.bool = true
object = object2
}
}
控制台输出:
PAGE 1 NUMBER: 5
PAGE 1 STRING: Hello from View 1
PAGE 1 OBJECT NUMBER: 5
PAGE 1 OBJECT STRING: Hello from View 1
PAGE 1 OBJECT BOOL: false
PAGE 2 NUMBER: 5
PAGE 2 STRING: Hello from View 1
PAGE 2 OBJECT NUMBER: 5
PAGE 2 OBJECT STRING: Hello from View 1
PAGE 2 OBJECT BOOL: false
PAGE 1 NUMBER: 5
PAGE 1 STRING: Hello from View 1
PAGE 1 OBJECT NUMBER: 12
PAGE 1 OBJECT STRING: Hello back from View 2
PAGE 1 OBJECT BOOL: true
我已经搜索了几天的答案(如果之前已经回答,我会很感激链接)。
由于尚未有人回答您的问题,我将在此处添加我的答案。
But I noticed when working with a custom class this was not the case. If I changed values that belonged to that class, they would retain when going back to the first view. I did notice that if I tried to replace the whole class with a completely new one, that would not take, which would be similar to trying to change a whole Int, but why would only changing the class' properties still work in that case?
这种行为的原因是因为如注释中所述,类是引用类型,因此您为其属性设置的任何值在您使用它的同一实例的所有地方都可用。 IMO,这是类在 swift 中对抗结构的一大优势。如果您不希望这种行为,您可以考虑将 Object
更改为结构类型。
在您的情况下,即使您在 SecondViewController
中初始化 var object = Object()
,您也正在用您的 object
替换该实例ViewController
中的实例作为 prepareForSegue
方法中的 vc.object = object
。由于您正在执行此操作,因此当您返回上一屏幕时,您对对象属性所做的任何更改都将保留。
关于ios - Swift - 为什么在返回前一个 View 时传递的自定义类保留更改的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45686186/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) | Powered by Discuz! X3.4 |