Inout 不使用 SubClass 对象抛出无法将不可变值作为 inout 参数传递
Inout not working with SubClass object throws Cannot pass immutable value as inout argument
我有两段代码
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 错误或者我遗漏了什么?
由于平面 class 是用 "init(_ anchor: ARPlaneAnchor)" 初始化的,即使它被声明为 SCNNode class 它 returns 与块 2 中的纯 SCNNode 不同的实例.
我不是变异主题的专家,但认为 Apple 博客中有一份有趣的文档 The Role of Mutation in Safety
不太确定,但由于 class 本质上是可变的,您可能可以将更新功能移动到平面 class 作为(测试)解决方案
Inout 不适用于 Subclass
这是例子
class SuperClass {
var name = "Prashant"
}
class TestObject:SuperClass {
}
func updateTemp ( object:inout SuperClass) {
object.name = "P.T"
}
现在,当您创建 TestObject
对象时,它是 SuperClass
的子class 对象,它将不允许这样做。
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
因为 class 是引用类型
3) 像这样创建泛型函数(泛型很棒!!)
func updateTemp<T:SuperClass> ( object:inout T) {
object.name = "P.T"
}
希望对大家有帮助
我有两段代码
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 错误或者我遗漏了什么?
由于平面 class 是用 "init(_ anchor: ARPlaneAnchor)" 初始化的,即使它被声明为 SCNNode class 它 returns 与块 2 中的纯 SCNNode 不同的实例.
我不是变异主题的专家,但认为 Apple 博客中有一份有趣的文档 The Role of Mutation in Safety
不太确定,但由于 class 本质上是可变的,您可能可以将更新功能移动到平面 class 作为(测试)解决方案
Inout 不适用于 Subclass
这是例子
class SuperClass {
var name = "Prashant"
}
class TestObject:SuperClass {
}
func updateTemp ( object:inout SuperClass) {
object.name = "P.T"
}
现在,当您创建 TestObject
对象时,它是 SuperClass
的子class 对象,它将不允许这样做。
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
因为 class 是引用类型
3) 像这样创建泛型函数(泛型很棒!!)
func updateTemp<T:SuperClass> ( object:inout T) {
object.name = "P.T"
}
希望对大家有帮助