Search code examples
swiftarkitinout

Inout not working with SubClass object throws Cannot pass immutable value as inout argument


I have two block of codes

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
  // BLOCK 1 Which is not working 
    guard let planeAnchor = anchor as? ARPlaneAnchor else { return }

    var plane = Plane(with: planeAnchor) //IT IS SUBCLASS OF SCNNode
    var geo = plane.geometry
    plane.transform = SCNMatrix4MakeRotation(-.pi / 2, 1, 0, 0)

    update(&plane, withGeometry: plane.geo, type: .static)

   //Up here Cannot pass immutable value as inout argument: implicit conversion from 'Plane' to 'SCNNode' requires a temporary

    node.addChildNode(plane)

   // BLOCK 2 Which is working 


    let width = CGFloat(planeAnchor.extent.x)
    let height = CGFloat(planeAnchor.extent.z)
    let plane1 = SCNPlane(width: width, height: height)

    plane1.materials.first?.diffuse.contents = UIColor.white.withAlphaComponent(0.5)

    var planeNode = SCNNode(geometry: plane1)

    let x = CGFloat(planeAnchor.center.x)
    let y = CGFloat(planeAnchor.center.y)
    let z = CGFloat(planeAnchor.center.z)
    planeNode.position = SCNVector3(x,y,z)
    planeNode.eulerAngles.x = -.pi / 2

    update(&planeNode, withGeometry: plane1, type: .static)
    // WORKING FINE

    node.addChildNode(planeNode)

    self.planes[anchor.identifier] = plane

}

BLOCK1

I have subclass class Plane: SCNNode when I try to pass it's object to function which required inout it shows me error

Cannot pass immutable value as inout argument: implicit conversion from 'Plane' to 'SCNNode' requires a temporary

While if I remove subclass then it is working fine

Why this is it swift bug or I am missing anything?


Solution

  • Since the Plane class is initialized with "init(_ anchor: ARPlaneAnchor)" even it is declared as SCNNode class it returns a different instance than a pure SCNNode as in the Block 2.

    I'm not an expert on the mutation subject but think there is an interesting document in Apple blogs The Role of Mutation in Safety

    Not really sure but since a class is by nature mutable you probably can move the update function to the Plane class as a (test) solution