我正在将我在 Objective c 中制作的游戏移植到 swift,因为这是学习如何移植某些东西以及如何将 swift 与 objective c 以及 objective c 与 swift 混合的好方法。
但经过大量搜索后,我找不到如何移植这行代码:
GCControllerDirectionPadValueChangedHandler dpadMoveHandler =
^(GCControllerDirectionPad *dpad, float xValue, float yValue) { }
我唯一找到的是苹果的这段代码:
swift
typealias GCControllerDirectionPadValueChangedHandler =
(GCControllerDirectionPad!, CFloat, CFloat) -> Void
Objective-C
typedef void (^GCControllerDirectionPadValueChangedHandler)
(GCControllerDirectionPad *dpad, float xValue, float yValue)
--
这是我迄今为止尝试过的代码,但没有成功。
var dpadMoveHandler: GCControllerDirectionPadValueChangedHandler =
(#dpad:GCControllerDirectionPad, #xValue:CFloat, #yValue:CFloat) {
}
最佳答案
该行声明了一个 Objc block 。您在 Swift 中看到的类型别名是闭包类型。等效的代码是这样的:
var dpadMoveHandler:GCControllerDirectionPadValueChangedHandler =
{
(dpad:GCControllerDirectionPad!, xValue:CFloat, yValue:CFloat) -> () in
return
}
阅读有关 Swift 闭包的信息 here .
关于objective-c - 将 Objective-C block 移植到 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24296283/