0

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];
asked Apr 30, 2015 at 17:10
1
  • So basically, a cast? Commented Apr 30, 2015 at 18:38

1 Answer 1

2

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
}
answered Apr 30, 2015 at 18:43
Sign up to request clarification or add additional context in comments.

4 Comments

It's an SKNode from collision. So does the same apply?
Is TapCircleIcon a subclass of SKNode? If it isn't, SKNode cannot be casted to TapCircleIcon.
Yes tapcricleicon is subclass of SKNode
Basically this is a failable cast. If the 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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.