I am struggling to find the answer for this simply because I am unsure what to search for.
In objective-C I would do something like this to get a pointer to an object of a specific class:
CustomLabelClass *detailLabel = (CustomLabelClass *)[sortCell.contentView viewWithTag:kSortDetailLabelTag];
I would like to do the same in Swift. Basically I detect collision in Sprite Kit Scene and then try and pass the Node (which I know is of a specific class to another function).
if ((contact.bodyA.node?.isKindOfClass(TapCircleIcon)) != nil) {
updateOnScreenStatusFor(...)
I need to pass the TapCircleIcon into the function replacing '...'. So in Obj-c I would do something like:
TapCircleIcon *tapCircle = (TapCircleIcon *)[contact.bodyA.node];
-
So basically, a cast?Nate Lee– Nate Lee2015年04月30日 18:38:31 +00:00Commented Apr 30, 2015 at 18:38
1 Answer 1
You no longer need isKindOfClass
in Swift. I am assuming that node
is an AnyObject?
optional. You can cast it to TapCircleIcon
and unwrap the optional using this if statement.
if let tapCircleIcon = contact.bodyA.node as? TapCircleIcon {
updateOnScreenStatusFor(tapCircleIcon)
} else {
// node is not a TapCircleIcon
}
4 Comments
node as? TapCircleIcon
fails to cast node to TapCircleIcon class, then the else statement will be called. If it is successful, then it will call updateOnScreenStatusFor()
. This should be safe coding regardless of what node is.