1

我有两个代码块

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

}

块1

class Plane: SCNNode 当我尝试将它的对象传递给需要inout它向我显示错误的函数时,我有子类

无法将不可变值作为 inout 参数传递:从“Plane”到“SCNNode”的隐式转换需要临时

虽然如果我删除子类,那么它工作正常

为什么这是快速错误或我遗漏了什么?

4

2 回答 2

1

Inout 不适用Subclass

这是示例

class SuperClass {
    var name = "Prashant"

}

class TestObject:SuperClass {
}


func updateTemp ( object:inout SuperClass) {
    object.name = "P.T"
}

现在,当您创建TestObject它的子类的对象时SuperClass,将不允许这样做。

var obj  = TestObject()
self.updateTemp(object: &obj) // Cannot pass immutable value as inout argument: implicit conversion from 'TestObject' to 'SuperClass' requires a temporary
print(obj.name)

如何解决这个问题

三种方式

1)创建对象 var obj:SuperClass = TestObject()

2)这实际上不需要inout因为类是引用类型

3)创建类似的通用函数(通用很棒!!)

func updateTemp<T:SuperClass> ( object:inout T) {
    object.name = "P.T"
} 

希望对某人有帮助

于 2018-06-29T06:18:23.520 回答
0

由于 Plane 类是用“init(_anchor: ARPlaneAnchor)”初始化的,即使它被声明为 SCNNode 类,它也会返回一个与块 2 中的纯 SCNNode 不同的实例。

我不是突变主题的专家,但我认为 Apple 博客中有一篇有趣的文档The Role of Mutation in Safety

不太确定,但由于一个类本质上是可变的,您可能可以将更新功能移动到 Plane 类作为(测试)解决方案

于 2018-06-28T16:50:01.023 回答