[英]How to create a pointer/pass parameter to function in Swift
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: 在Objective-C中,我将执行以下操作以获取指向特定类的对象的指针:
CustomLabelClass *detailLabel = (CustomLabelClass *)[sortCell.contentView viewWithTag:kSortDetailLabelTag];
I would like to do the same in Swift. 我想在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). 基本上,我在Sprite Kit场景中检测到碰撞,然后尝试将Node(我知道属于特定类的Node)传递给另一个函数。
if ((contact.bodyA.node?.isKindOfClass(TapCircleIcon)) != nil) {
updateOnScreenStatusFor(...)
I need to pass the TapCircleIcon into the function replacing '...'. 我需要将TapCircleIcon传递给替换“ ...”的函数。 So in Obj-c I would do something like: 因此,在Obj-c中,我将执行以下操作:
TapCircleIcon *tapCircle = (TapCircleIcon *)[contact.bodyA.node];
You no longer need isKindOfClass
in Swift. 您不再需要Swift中的isKindOfClass
。 I am assuming that node
is an AnyObject?
我假设该node
是AnyObject?
optional. 可选的。 You can cast it to TapCircleIcon
and unwrap the optional using this if statement. 您可以将其TapCircleIcon
为TapCircleIcon
并使用此if语句解开可选内容。
if let tapCircleIcon = contact.bodyA.node as? TapCircleIcon {
updateOnScreenStatusFor(tapCircleIcon)
} else {
// node is not a TapCircleIcon
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.