我有两段代码
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
我有子类 class Plane: SCNNode
当我尝试将它的对象传递给需要 inout
的函数时它显示错误
Cannot pass immutable value as inout argument: implicit conversion from 'Plane' to 'SCNNode' requires a temporary
虽然如果我删除子类那么它工作正常
为什么这是 swift 错误或者我遗漏了什么?
最佳答案
Inout 不适用于 Subclass
这里是例子
class SuperClass {
var name = "Prashant"
}
class TestObject:SuperClass {
}
func updateTemp ( object:inout SuperClass) {
object.name = "P.T"
}
现在,当您创建作为 SuperClass
子类的 TestObject
对象时,它将不允许这样做。
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"
}
希望对大家有帮助
关于swift - Inout 不使用 SubClass 对象抛出无法将不可变值作为 inout 参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51082452/