title: How can I reduce 3 blocks of code to 1 in iOS
body: <p>I created a fun little AR app that allows me to point my phone at index cards, that contain 2D images of Christmas items drawn by my niece, and have 3D versions of the images pop up.</p>
  <p>However, I have limited my app to just 3 images for now, as each picture/3D combo has its own block of code (shown below) but I would like to somehow consolidate to one block (even if I need to rename the image files to a numbered format, but would appreciate any advice).</p>
  <p>Screenshots of the app, using the &quot;3 blocks of code method&quot; are included.</p>
  <p><a href="https://i.stack.imgur.com/3HsAG.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3HsAG.jpg" alt="Before" /></a>
  <a href="https://i.stack.imgur.com/qreln.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qreln.png" alt="After" /></a></p>
  <pre><code>    // MARK: - ARSCNViewDelegate
      
      //the anchor will be the image detected and the node will be a 3d object 
      func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -&gt; SCNNode? {
          
          //the 3d object
          let node = SCNNode()
          
          //if this detects a plane or point or anything other then an image, will not run this code
          if let imageAnchor = anchor as? ARImageAnchor {
              
              
              //this plane is created from the image detected (card).  want the width and height in the physical world to be the same as the card, so effectively saying &quot;look at the anchor image found, and get its size properties”
              let plane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, height: imageAnchor.referenceImage.physicalSize.height)
              
              //once dimension is given to the plane, use it to create a planenode which is a 3d object that will be rendered on top of the card
              let planeNode = SCNNode(geometry: plane)
              
              //makes the plane translucent
              plane.firstMaterial?.diffuse.contents = UIColor(white: 1.0, alpha: 0.5)
              
              //by default the plane created is vertical so need to flip it to be flat with the card detected
              planeNode.eulerAngles.x = -.pi / 2
              
              
              // tap into the node created above and add a child node which will be the plane node
              node.addChildNode(planeNode)
              
               // ------------3 BLOCKS OF CODE BELOW THAT NEED TO SIMPLIFY TO 1----------------
               // This is the first block of code for the ChristmasTree.png image/ChristmasTree.scn 3D object – note that the 2D image is not detected if “.png” is included at the end of the image anchor, however the “.scn” appears to be required for the 3D object).
              if imageAnchor.referenceImage.name == &quot;ChristmasTree&quot; {
                  
                  //create the 3d model on top of the card
                  if let cardScene = SCNScene(named: &quot;art.scnassets/ChristmasTree.scn&quot;) {
                      
                      //create a node that will represent the 3d object.
                      if let cardNode = cardScene.rootNode.childNodes.first {
                          
                          //Since the 3d image is rotated the other way, need to bring it forward so same as above only with positive
                          cardNode.eulerAngles.x = .pi / 2
                          
                          planeNode.addChildNode(cardNode)
                      }
                  }
              }
              
             // This is the second block of code for the Gift.png image/Gift.scn 3D object
              if imageAnchor.referenceImage.name == &quot;Gift&quot; {
                  
                  //create the 3d model on top of the card
                  if let cardScene = SCNScene(named: &quot;art.scnassets/Gift.scn&quot;) {
                      
                      //create a node that will represent the 3d object
                      if let cardNode = cardScene.rootNode.childNodes.first {
                          
                          //Since the 3d image is rotated the other way, need to bring it forward so same as above only with positive
                          cardNode.eulerAngles.x = .pi / 2
                          
                          planeNode.addChildNode(cardNode)
                      }
                  }
              }
              
              // This is the third block of code for the GingerbreadMan.png image/GingerbreadMan.scn 3D object
              if imageAnchor.referenceImage.name == &quot;GingerbreadMan&quot; {
                  
                  //create the 3d model on top of the card
                  if let cardScene = SCNScene(named: &quot;art.scnassets/GingerbreadMan.scn&quot;) {
                      
                      //create a node that will represent the 3d object
                      if let cardNode = cardScene.rootNode.childNodes.first {
                          
                          //Since the 3d image is rotated the other way, need to bring it forward so same as above only with positive
                          cardNode.eulerAngles.x = .pi / 2
                          
                          planeNode.addChildNode(cardNode)
                      }
                  }
              }
          }
          
          //this method is expecting an output of SCNNode and it needs to send that back onto the scene so it can render the 3d object
          return node
      }
      
  }
  </code></pre>
  <p>EDIT</p>
  <p>Using Vadian's recommendation, I have revised the code as follows, however it has resulted in only the translucent plane appearing when pointing the camera at the index card:</p>
  <pre><code>// MARK: - ARSCNViewDelegate
      
      func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -&gt; SCNNode? {
          
          let node = SCNNode()
          
          if let imageAnchor = anchor as? ARImageAnchor {
              
              let plane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, height: imageAnchor.referenceImage.physicalSize.height)
              
              let planeNode = SCNNode(geometry: plane)
           
              plane.firstMaterial?.diffuse.contents = UIColor(white: 1.0, alpha: 0.5)
              
              planeNode.eulerAngles.x = -.pi / 2
              
              node.addChildNode(planeNode)
              
              //------------single block of code------------
  
              let name = imageAnchor.referenceImage.name
              if [&quot;ChristmasTree&quot;, &quot;Gift&quot;, &quot;GingerbreadMan&quot;].contains(name) {
                      
                  if let cardScene = SCNScene(named: &quot;art.scnassets/\(name).scn&quot;) {
                          
                      if let cardNode = cardScene.rootNode.childNodes.first {
                              
                          cardNode.eulerAngles.x = .pi / 2
                              
                          planeNode.addChildNode(cardNode)
                      }
                  }
              }
              //------------single block of code------------
          }
  
          return node
      }
      
  }
  </code></pre>