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.
Based on your code:
// Custom node class
class GroundNode: SKNode {
weak var entity: GKEntity! = GKEntity() // or some custom initialization...
}
// Where I am getting the error
func setupNode() {
var ground: GroundNode
ground = returnNode() as? GroundNode
}
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 of SKNode
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 of SKNode
's subclass - GroundNode
which is not appropriate in OOP.
See Dynamic Binding OOP concept:
In object- oriented programming languages, a variable of a superclass
type can contain a subclass instance.
(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 of SKNode
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.
import GameplayKit
import SpriteKit
// Custom node class
class GroundNode: SKNode {
var entity: GKEntity = GKEntity()
}
// Return node function
func returnNode() -> SKNode {
// let's assume that you are correctly getting an instance
// of GroundNode class here, and return it from the function
// as of SKNode type
let returnNode: SKNode = GroundNode()
return returnNode
}
func setupNode() {
var groundNode: GroundNode?
// Type Casting
// Approach #1
let someNode = returnNode()
if someNode is GroundNode {
groundNode = someNode as! GroundNode
}
// Approach #2
groundNode = returnNode() as? GroundNode
print("groundNode = \(groundNode)")
}
I think it should be
func returnNode() -> GroundNode {