<small id='4v38a'></small><noframes id='4v38a'>

  • <tfoot id='4v38a'></tfoot>

      1. <i id='4v38a'><tr id='4v38a'><dt id='4v38a'><q id='4v38a'><span id='4v38a'><b id='4v38a'><form id='4v38a'><ins id='4v38a'></ins><ul id='4v38a'></ul><sub id='4v38a'></sub></form><legend id='4v38a'></legend><bdo id='4v38a'><pre id='4v38a'><center id='4v38a'></center></pre></bdo></b><th id='4v38a'></th></span></q></dt></tr></i><div id='4v38a'><tfoot id='4v38a'></tfoot><dl id='4v38a'><fieldset id='4v38a'></fieldset></dl></div>

          <bdo id='4v38a'></bdo><ul id='4v38a'></ul>
      2. <legend id='4v38a'><style id='4v38a'><dir id='4v38a'><q id='4v38a'></q></dir></style></legend>
      3. Swift - 迭代结构对象时如何对其进行变异

        时间:2024-04-15
            <tbody id='OU5ut'></tbody>

            <tfoot id='OU5ut'></tfoot>

              <legend id='OU5ut'><style id='OU5ut'><dir id='OU5ut'><q id='OU5ut'></q></dir></style></legend>

                <i id='OU5ut'><tr id='OU5ut'><dt id='OU5ut'><q id='OU5ut'><span id='OU5ut'><b id='OU5ut'><form id='OU5ut'><ins id='OU5ut'></ins><ul id='OU5ut'></ul><sub id='OU5ut'></sub></form><legend id='OU5ut'></legend><bdo id='OU5ut'><pre id='OU5ut'><center id='OU5ut'></center></pre></bdo></b><th id='OU5ut'></th></span></q></dt></tr></i><div id='OU5ut'><tfoot id='OU5ut'></tfoot><dl id='OU5ut'><fieldset id='OU5ut'></fieldset></dl></div>
                • <bdo id='OU5ut'></bdo><ul id='OU5ut'></ul>
                • <small id='OU5ut'></small><noframes id='OU5ut'>

                  本文介绍了Swift - 迭代结构对象时如何对其进行变异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我仍然不确定结构复制或引用的规则.

                  I am still not sure about the rules of struct copy or reference.

                  我想在从数组迭代结构对象时对其进行变异:例如在这种情况下,我想更改背景颜色但是编译器对我大喊大叫

                  I want to mutate a struct object while iterating on it from an array: For instance in this case I would like to change the background color but the compiler is yelling at me

                  struct Options {
                    var backgroundColor = UIColor.blackColor()
                  }
                  
                  var arrayOfMyStruct = [MyStruct]
                  
                  ...
                  
                  for obj in arrayOfMyStruct {
                    obj.backgroundColor = UIColor.redColor() // ! get an error
                  }
                  

                  推荐答案

                  struct 是值类型,因此在 for 循环中你正在处理一个副本.

                  struct are value types, thus in the for loop you are dealing with a copy.

                  作为一个测试,你可以试试这个:

                  Just as a test you might try this:

                  struct Options {
                     var backgroundColor = UIColor.black
                  }
                  
                  var arrayOfMyStruct = [Options]()
                  
                  for (index, _) in arrayOfMyStruct.enumerated() {
                     arrayOfMyStruct[index].backgroundColor = UIColor.red
                  }
                  

                  斯威夫特 2:

                  struct Options {
                      var backgroundColor = UIColor.blackColor()
                  }
                  
                  var arrayOfMyStruct = [Options]()
                  
                  for (index, _) in enumerate(arrayOfMyStruct) {
                      arrayOfMyStruct[index].backgroundColor = UIColor.redColor() 
                  }
                  

                  这里你只是枚举索引,直接访问存储在数组中的值.

                  Here you just enumerate the index, and access directly the value stored in the array.

                  希望这会有所帮助.

                  这篇关于Swift - 迭代结构对象时如何对其进行变异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用 Swift 将文本复制到剪贴板/粘贴板 下一篇:使用 iOS 将文本复制到剪贴板

                  相关文章

                    <bdo id='s6PFX'></bdo><ul id='s6PFX'></ul>

                    <legend id='s6PFX'><style id='s6PFX'><dir id='s6PFX'><q id='s6PFX'></q></dir></style></legend>

                  1. <small id='s6PFX'></small><noframes id='s6PFX'>

                  2. <tfoot id='s6PFX'></tfoot>
                  3. <i id='s6PFX'><tr id='s6PFX'><dt id='s6PFX'><q id='s6PFX'><span id='s6PFX'><b id='s6PFX'><form id='s6PFX'><ins id='s6PFX'></ins><ul id='s6PFX'></ul><sub id='s6PFX'></sub></form><legend id='s6PFX'></legend><bdo id='s6PFX'><pre id='s6PFX'><center id='s6PFX'></center></pre></bdo></b><th id='s6PFX'></th></span></q></dt></tr></i><div id='s6PFX'><tfoot id='s6PFX'></tfoot><dl id='s6PFX'><fieldset id='s6PFX'></fieldset></dl></div>