<legend id='p2D0o'><style id='p2D0o'><dir id='p2D0o'><q id='p2D0o'></q></dir></style></legend>
      <bdo id='p2D0o'></bdo><ul id='p2D0o'></ul>

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

    3. <tfoot id='p2D0o'></tfoot>

      在 Swift 初始化期间调用实例方法

      时间:2023-09-08

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

              <bdo id='T7Pzw'></bdo><ul id='T7Pzw'></ul>
            • <small id='T7Pzw'></small><noframes id='T7Pzw'>

                  <tbody id='T7Pzw'></tbody>
              1. <tfoot id='T7Pzw'></tfoot>
                本文介绍了在 Swift 初始化期间调用实例方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我是 Swift 新手,想用这样的实例方法初始化对象的成员变量:

                I am new to Swift and would like to initialize an object's member variable using an instance method like this:

                class MyClass {
                  var x: String
                  var y: String
                
                  func createY() -> String {
                    self.y = self.x + "_test" // this computation could be much more complex
                  }
                
                  init(x: String) {
                    self.x = x
                    self.y = self.createY()
                  }     
                }
                

                基本上,我不想在 init 方法中内联所有初始化代码,而是想将 y 的初始化代码提取到专用方法 createY 并在 init 中调用此实例方法 createY.但是,Swift 编译器(Xcode 6.3 beta 中的 Swift 1.2 编译器)抱怨:

                Basically, instead of inlining all the initialization code in init method, I want to extract the initialization code of y to a dedicated method createY and call this instance method createY in init. However, Swift compiler (Swift 1.2 compiler in Xcode 6.3 beta) complains:

                在super.init初始化self之前在方法调用'xxx'中使用'self'

                use of 'self' in method call 'xxx' before super.init initialize self

                这里的xxx"是实例方法的名称(createY).

                Here 'xxx' is the name of the instance method (createY).

                我可以理解 Swift 编译器在抱怨什么以及它想要解决的潜在问题.但是,我不知道如何解决它.在 Swift 中调用 init 中初始化代码的其他实例方法的正确方法应该是什么?

                I can understand what Swift compiler is complaining and the potential problem it wants to address. However, I have no idea how to fix it. What should be the correct way in Swift to call other instance method of initialization code in init?

                目前,我使用以下技巧作为解决方法,但我认为这不是解决此问题的惯用方法(并且此解决方法需要使用 var 声明 ycode> 而不是 let 这让我也感到不安):

                Currently, I use the following trick as work around but I don't think this is the idiomatic solution to this problem (and this workaround requires y to be declared using var instead of let which makes me feel uneasy too):

                init(x: String) {
                  self.x = x
                  super.init()
                  self.y = createY()
                } 
                

                感谢任何评论.谢谢.

                推荐答案

                createY() 转换为接受 x 作为参数并返回一个全局或类函数y.

                Convert createY() to a global or class function that accepts x as an argument and returns a y.

                func createY(x: String) -> String {
                    return x + "_test" // this computation could be much more complex
                }
                

                然后从你的 init 正常调用它.

                Then just call it normally from your init.

                class MyClass {
                  let x: String
                  let y: String
                
                  init(x: String) {
                    self.x = x
                    self.y = createY(x)
                  }     
                }
                

                这篇关于在 Swift 初始化期间调用实例方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Objective-C中的静态构造函数等效? 下一篇:为什么初始化子类需要调用超类的相同init函数?

                相关文章

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

                <small id='x77ic'></small><noframes id='x77ic'>

                <legend id='x77ic'><style id='x77ic'><dir id='x77ic'><q id='x77ic'></q></dir></style></legend>
                    <bdo id='x77ic'></bdo><ul id='x77ic'></ul>
                  <tfoot id='x77ic'></tfoot>