title: How to use Swift extensions functions inside initializers?
body: <p>I'm trying to extend my <strong>SKScene</strong> Swift classes to create a <strong>SpriteKit Node</strong> using the textures from an loaded atlas and set the node's name at the same time saving me the lines I need to use for every other node, and I'm using it as extension because I don't want to write the same method in every class I make. Also, I have to create the method as class method or I get the same error at init.</p>
  <p>This is my extension's code:</p>
  <pre><code>import SpriteKit
  
  internal extension SKScene {
  
  func nodeWithTexture(atlas: SKTextureAtlas, texture: String, name: String) -&gt; SKSpriteNode {
      let nodeTexture = atlas.textureNamed(texture)
      let node = SKSpriteNode(texture: nodeTexture)
      node.name = name
      return node
    }
  }
  </code></pre>
  <p>Now, my problem with this is that I can't use the method in my scene's initilizer. It's giving me a super.init error that I don't understand.</p>
  <p>This is the error I got when I try to use the extension inside the init before the <code>super.init(size: size)</code> call.</p>
  <p><img src="https://i.stack.imgur.com/zrTHo.png" alt="enter image description here" /></p>
  <p>And this one is the error I got when I try to use it after super.init(size) call.</p>
  <p><img src="https://i.stack.imgur.com/CRrXr.png" alt="enter image description here" /></p>
  <h2>Question Update</h2>
  <p>I workaround my problem using the next approach thanks to Nate's answer.</p>
  <p>First I initialize all the stored properties at the declaration moment:</p>
  <pre><code>let background = SKSpriteNode()
  </code></pre>
  <p>Then I call the extension method after the <code>super.init(size: size)</code> call and everything is set:</p>
  <pre><code>super.init(size: size)
  background = nodeWithTexture(atlas, texture: &quot;bg&quot;, name: &quot;background&quot;)
  </code></pre>