gpt4 book ai didi

ios - Swift - 为什么传递的自定义类在返回到上一个 View 时保留更改的属性?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:08:30 26 4
gpt4 key购买 nike

当通过 segue 传递数据时,我注意到一些奇怪的事情,我只是想不通为什么会这样。我知道我很可能误解了类的性质,所以如果我能得到一些帮助来了解它,我将不胜感激。

当我通过 segue(都连接到导航 Controller )将 Int 或 String 传递给第二个 View 时,然后在第二个 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("PAGE 1 NUMBER: ", number)
print("PAGE 1 STRING: ", string)
print("PAGE 1 OBJECT NUMBER: ", object.number)
print("PAGE 1 OBJECT STRING: ", object.string)
print("PAGE 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("PAGE 2 NUMBER: ", number)
print("PAGE 2 STRING: ", string)
print("PAGE 2 OBJECT NUMBER: ", object.number)
print("PAGE 2 OBJECT STRING: ", object.string)
print("PAGE 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 中的实例作为 vc.object = objectprepareForSegue 方法中。由于您正在执行此操作,因此当您返回上一屏幕时,您对对象属性所做的任何更改都将保留。

关于ios - Swift - 为什么传递的自定义类在返回到上一个 View 时保留更改的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45686186/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com