菜鸟教程小白 发表于 2022-12-11 18:30:14

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


                                            <p><p>我在通过 segue 传递数据时发现了一些奇怪的东西,我只是不明白为什么会这样。我知道我很可能对类(class)的性质有一些误解,所以如果我能获得一些关于它的知识的帮助,我将不胜感激。</p>

<p>当我通过 segue(都连接到导航 Controller )将 Int 或 String 传递给第二个 View 时,然后在第二个 View 中更改该值,然后返回到第一个,该 Int 的值或字符串将与最初在第一个 View 中设置的相同,不保留在第二个 View 中更改的值。这是有道理的,也是我所期望的。</p>

<p>但我注意到在使用自定义类时情况并非如此。如果我更改了属于该类的值,它们会在返回第一个 View 时保留。我确实注意到,如果我尝试用一​​个全新的类替换整个类,那将不会发生,这类似于尝试更改整个 Int,但为什么在这种情况下仅更改类的属性仍然有效? </p>

<p>我确实知道如何将数据传回之前的 View ,这样我可以更好地理解<em>为什么</em>事情会这样工作。</p>

<p>我一直在玩它,这里的代码可以更好地解释我在说什么:</p>

<p>第一个 ViewController :</p>

<pre><code>class ViewController: UIViewController {

    var number = 5
    var string = &#34;Hello from View 1&#34;
    var object = Object(number: 5, string: &#34;Hello from View 1&#34;, bool: false)

    override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)

      print(&#34;PAGE 1 NUMBER: &#34;, number)
      print(&#34;PAGE 1 STRING: &#34;, string)
      print(&#34;PAGE 1 OBJECT NUMBER: &#34;, object.number)
      print(&#34;PAGE 1 OBJECT STRING: &#34;, object.string)
      print(&#34;PAGE 1 OBJECT BOOL: &#34;, object.bool)
    }

    @IBAction func buttonPress(_ sender: Any) {
      self.performSegue(withIdentifier: &#34;toSecond&#34;, sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if segue.identifier == &#34;toSecond&#34; {
            let vc = segue.destination as! SecondViewController
            vc.number = number
            vc.string = string
            vc.object = object
      }
    }
}
</code></pre>

<p>第二个 ViewController :</p>

<pre><code>class SecondViewController: UIViewController {

    var number = 0
    var string = &#34;&#34;
    var object = Object()

    override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)

      print(&#34;PAGE 2 NUMBER: &#34;, number)
      print(&#34;PAGE 2 STRING: &#34;, string)
      print(&#34;PAGE 2 OBJECT NUMBER: &#34;, object.number)
      print(&#34;PAGE 2 OBJECT STRING: &#34;, object.string)
      print(&#34;PAGE 2 OBJECT BOOL: &#34;, object.bool)

      number = 12
      string = &#34;Hello back from View 2&#34;

      object.number = 12
      object.string = &#34;Hello back from View 2&#34;
      object.bool = true
      object = object2
    }
}
</code></pre>

<p>控制台输出:</p>

<pre><code>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
</code></pre>

<p>我已经搜索了几天的答案(如果之前已经回答,我会很感激链接)。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>由于尚未有人回答您的问题,我将在此处添加我的答案。 </p>

<blockquote>
<p>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&#39; properties still work in that case?</p>
</blockquote>

<p>这种行为的原因是因为如注释中所述,类是引用类型,因此您为其属性设置的任何值在您使用它的同一实例的所有地方都可用。 IMO,这是类在 swift 中对抗结构的一大优势。如果您不希望这种行为,您可以考虑将 <code>Object</code> 更改为结构类型。 </p>

<p>在您的情况下,即使您在 <code>SecondViewController</code> 中初始化 <code>var object = Object()</code>,您也正在用您的 <code>object</code> 替换该实例<code>ViewController</code> 中的实例作为 <code>prepareForSegue</code> 方法中的 <code>vc.object = object</code>。由于您正在执行此操作,因此当您返回上一屏幕时,您对对象属性所做的任何更改都将保留。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Swift - 为什么在返回前一个 View 时传递的自定义类保留更改的属性?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/45686186/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/45686186/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Swift - 为什么在返回前一个 View 时传递的自定义类保留更改的属性?