swift - 在 swift 中将带有参数的函数分配给变量

标签 swift sprite-kit

我使用此代码创建了一个按钮类

http://nathandemick.com/programming/tutorial/2014/09/23/buttons-sprite-kit-using-swift.html

但是action变量没有参数 如果我更改 init 和操作

 var action: (id: Int) -> Void

之后我无法执行touchesEnded方法中的函数

func test(){
    println("test");
}

func testTwo(id: Int){
    println("testTwo");
}


var action: () -> Void;
action = test
action() // prints test

var actionTwo: (id: Int) -> Void;
actionTwo = testTwo

// This work
actionTwo(id: 9); // prints testTwo

// But I want this to work
actionTwo();

最佳答案

您需要为您的函数提供一个定义,使整数参数成为可选的。

func testTwo(id: Int? = nil) {
   print("something")
}

关于swift - 在 swift 中将带有参数的函数分配给变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31086544/

相关文章:

swift - getStream 中文件模型的访问级别问题

swift - 按下时使球弹跳

ios - Xcode,目标类模板缺少SKScene的子类

ios - 在 SpriteKit 中的 GameScene.swift 之前添加一个屏幕

ios - 如何通过 SKEffectNode 在 Sprite 周围创建光晕

ios - SpriteKit 和 SwiftUI 动画(内存泄漏 - NSXPCConnection?)

ios - Swift - 函数完成后如何调用函数?

javascript - Swift React Native 回调模块

ios - 当 tableview 使用自动布局滚动时避免粘性标题 View

ios - 在现有 iOS 应用程序中集成 react-native(0.40.0) 后找不到 yoga/Yoga.h 头文件