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

    <small id='2mhB2'></small><noframes id='2mhB2'>

      <bdo id='2mhB2'></bdo><ul id='2mhB2'></ul>

      <legend id='2mhB2'><style id='2mhB2'><dir id='2mhB2'><q id='2mhB2'></q></dir></style></legend>

      1. <tfoot id='2mhB2'></tfoot>

        使用 self 进行 Swift 惰性实例化

        时间:2023-09-08

        <small id='9yQP7'></small><noframes id='9yQP7'>

      2. <tfoot id='9yQP7'></tfoot>

              <bdo id='9yQP7'></bdo><ul id='9yQP7'></ul>

                <tbody id='9yQP7'></tbody>
              <legend id='9yQP7'><style id='9yQP7'><dir id='9yQP7'><q id='9yQP7'></q></dir></style></legend>

              1. <i id='9yQP7'><tr id='9yQP7'><dt id='9yQP7'><q id='9yQP7'><span id='9yQP7'><b id='9yQP7'><form id='9yQP7'><ins id='9yQP7'></ins><ul id='9yQP7'></ul><sub id='9yQP7'></sub></form><legend id='9yQP7'></legend><bdo id='9yQP7'><pre id='9yQP7'><center id='9yQP7'></center></pre></bdo></b><th id='9yQP7'></th></span></q></dt></tr></i><div id='9yQP7'><tfoot id='9yQP7'></tfoot><dl id='9yQP7'><fieldset id='9yQP7'></fieldset></dl></div>
                  本文介绍了使用 self 进行 Swift 惰性实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一些让我很困惑的事情,特别是以下代码触发了编译器错误未解析的标识符自身",我不确定为什么会发生这种情况,因为惰性意味着在使用该属性时,该类已经实例化了.我错过了什么吗?

                  I have something that really puzzles me, specifically the following code triggers a compiler error "unresolved identifier self", and I am not sure why this is happening, as lazy means that at the time the property will be used, the class is already instantiated. Am I missing something?

                  提前非常感谢.

                  这里是代码

                  class FirstClass {
                      unowned var second: SecondClass
                  
                      init(second:SecondClass) {
                          self.second = second
                          print("First reporting for duty")
                      }
                  
                      func aMethod() {
                          print("First's method reporting for duty")
                      }
                  }
                  
                  
                  class SecondClass {
                  
                      lazy var first = FirstClass(second: self)
                  
                      func aMethod() {
                          first.aMethod()
                      }
                  }
                  

                  推荐答案

                  由于某种原因,如果惰性属性需要显式类型注释初始值是指self.swift-evolution 邮件列表,但是我无法解释 为什么必要的.

                  For some reason, a lazy property needs an explicit type annotation if its initial value refers to self. This is mentioned on the swift-evolution mailing list, however I cannot explain why that is necessary.

                  lazy var first: FirstClass = FirstClass(second: self)
                  //            ^^^^^^^^^^^^
                  

                  您的代码按预期编译和运行.

                  your code compiles and runs as expected.

                  这是另一个例子,它证明了问题的发生也与 structs,即它与子类无关:

                  Here is another example which demonstrates that the problem occurs also with structs, i.e. it is unrelated to subclassing:

                  func foo(x: Int) -> Int { return x + 1 }
                  
                  struct MyClass {
                      let x = 1
                  
                      lazy var y = foo(0)            // No compiler error
                      lazy var z1 = foo(self.x)      // error: use of unresolved identifier 'self'
                      lazy var z2: Int = foo(self.x) // No compiler error
                  }
                  

                  y的初始值不依赖于self,不需要a类型注释.z1/z2的初始值依赖于self,并且只能使用显式类型注释进行编译.

                  The initial value of y does not depend on self and does not need a type annotation. The initial values of z1/z2 depend on self, and it compiles only with an explicit type annotation.

                  更新:这个已在 Swift 4 中修复/Xcode 9 测试版 3,惰性属性初始化器现在可以在没有显式 self 和显式类型注释的情况下引用实例成员.(感谢@hamish 的更新.)

                  Update: This has been fixed in Swift 4/Xcode 9 beta 3, lazy property initializers can now reference instance members without explicit self, and without explicit type annotation. (Thanks to @hamish for the update.)

                  这篇关于使用 self 进行 Swift 惰性实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Swift 中 UIViewController 的自定义初始化,并在情节提要中设置界面 下一篇:在未初始化的对象(空指针)上调用方法

                  相关文章

                1. <i id='uqBZj'><tr id='uqBZj'><dt id='uqBZj'><q id='uqBZj'><span id='uqBZj'><b id='uqBZj'><form id='uqBZj'><ins id='uqBZj'></ins><ul id='uqBZj'></ul><sub id='uqBZj'></sub></form><legend id='uqBZj'></legend><bdo id='uqBZj'><pre id='uqBZj'><center id='uqBZj'></center></pre></bdo></b><th id='uqBZj'></th></span></q></dt></tr></i><div id='uqBZj'><tfoot id='uqBZj'></tfoot><dl id='uqBZj'><fieldset id='uqBZj'></fieldset></dl></div>
                  <legend id='uqBZj'><style id='uqBZj'><dir id='uqBZj'><q id='uqBZj'></q></dir></style></legend>
                  1. <small id='uqBZj'></small><noframes id='uqBZj'>

                        <bdo id='uqBZj'></bdo><ul id='uqBZj'></ul>
                      <tfoot id='uqBZj'></tfoot>