I am returning an SKNode
from one function and I need to cast it to a custom SKNode
. I get the error Cannot assign value of
SKNodeto type
GroundNode. If I force cast, it compiles, but fails at runtime. What am I missing?
// Custom node class
class GroundNode: SKNode {
weak var entity: GKEntity!
}
// Return node function
func returnNode() -> SKNode {
...
return node
}
// Where I am getting the error
func setupNode() {
var ground: GroundNode
ground = returnNode() // error here.
//// ground = returnNode() as! GroundNode fails at runtime.
}
EDIT: I am getting an SKNode
from an sks file. My returnNode()
just get the child with name, and returns it to my setupNode()
function. I need to add the entity property, so I want to cast my returned SKNode
to a GroundNode
type.
I have seen this stackoverflow post.
This works with SKSpiteNode
, but apparently not with SKNode
, which does not make much sense to me.
If I cast my SKNode
from my sks file to a GroundNode
, it crashes at runtime.
setupNode
the call toreturnNode
actually returns aGroundNode
instance? It doesn't seems to be... Use the debugger to observe what really happens. – Jean-Baptiste Yunès