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.
I think it should be
func returnNode() -> GroundNode {
Based on your code:
This happened because returnNode output is a generic SKNode and you must explicit your casting to the subclassed GroundNode.
EDIT: Ok, with your update I think I've understand your issue, you've forgot to set the custom class for your GroundNode:
first
returnNode()
returns an instance ofSKNode
class. It might also return an instance of derived class (e.g.GroundNode
), but call site doesn't know that from the function declaration. You are trying to assign this instance to the variable ofSKNode
's subclass -GroundNode
which is not appropriate in OOP.See Dynamic Binding OOP concept:
(possibly, someone else could explain this in more details, but this is how things look to me)
second
Type Casting. When you suspect that some variable of a class type, might hold an instance of derived class, you could do a type casting in order to proof that.
returnNode()
returns an instance ofSKNode
OR any derived class, but the call site wants to process only instances of derived class (GroundNode
), hence one of type casting technique should be used.