简体   繁体   English

如何创建一个指针/传递参数以在Swift中运行

[英]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? 我假设该nodeAnyObject? optional. 可选的。 You can cast it to TapCircleIcon and unwrap the optional using this if statement. 您可以将其TapCircleIconTapCircleIcon并使用此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.

相关问题 如何在swift中将touchUpInside函数作为参数传递? - How to pass touchUpInside function as parameter in swift? 如何传递一个数组作为 Swift 中的参数? - How to pass an Array to function as a parameter in Swift? 如何将NSNotification作为参数传递给Swift中的选择器函数 - how to pass an NSNotification as parameter to the selector function in Swift 如何在 function Swift UI 中将视图作为参数传递 - How to pass view as Parameter in a function Swift UI 如何在Swift 3中为带有可选参数的函数创建选择器? - How to create selector for function with optional parameter in Swift 3? Swift:如何有选择地将函数作为参数传递并调用函数? - Swift: how to optionally pass function as parameter and invoke the function? 如何在 swift 4 中的 function 中动态地将结构的属性 select 作为参数传递? - How to select a property of a struct dynamically to pass as a parameter in a function in swift 4? 如何使用 swift 将 UIVIewController 名称作为参数传递给特定函数? - How to pass UIVIewController name as a parameter to particular function using swift? 如何在Swift中传递参数未知且返回值作为参数的函数 - How to pass a function with unknown parameters and returned value as a parameter in Swift 如何将UIViewController作为参数传递给swift 3? - How to pass UIViewController as a parameter swift 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM