gpt4 book ai didi

ios - SceneKit——将 Objective-C block 转换为 Swift

转载 作者:行者123 更新时间:2023-11-29 02:12:28 26 4
gpt4 key购买 nike

是否有 Swift 大师知道如何将 Objective-c 中的代码转换为 Swift?

SCNAnimationEventBlock chainEventBlock = ^(CAAnimation *animation, id animatedObject, BOOL playingBackward) {
[self.mainSkeleton addAnimation:secondAnim forKey:secondKey];
};

if (firstAnim.animationEvents == nil || firstAnim.animationEvents.count == 0) {
firstAnim.animationEvents = @[[SCNAnimationEvent animationEventWithKeyTime:fadeTime block:chainEventBlock]];
} else {
NSMutableArray *pastEvents = [NSMutableArray arrayWithArray:firstAnim.animationEvents];
[pastEvents addObject:[SCNAnimationEvent animationEventWithKeyTime:fadeTime block:chainEventBlock]];
firstAnim.animationEvents = pastEvents;
}

在 Swift 中,我尝试过:

var chainEventBlock : ((CAAnimation , AnyObject , Bool) -> (SCNAnimationEventBlock))?
chainEventBlock = { (animation, animatedObject, playingBackward)->(SCNAnimationEventBlock) in
return self.mainSkeleton?.addAnimation(anim2, forKey: secondKey)
}

if anim1?.animationEvents == nil || anim1?.animationEvents.count == 0 {
anim1?.animationEvents = [SCNAnimationEvent(keyTime: fadeTime, block: chainEventBlock)]
}

错误是:

enter image description here

谢谢。

最佳答案

您已将 block 的返回类型声明为 SCNAnimationEventBlock 但您没有返回它。

要么返回该类型的对象(不是可选的!),要么不返回任何内容并更改 chainEventBlock 的声明。

let chainEventBlock: SCNAnimationEventBlock = { animation, animatedObject, playingBackwards in
self.mainSkeleton?.addAnimation(anim2, forKey: secondKey)
return
}

if anim1?.animationEvents == nil || anim1?.animationEvents.count == 0 {
anim1?.animationEvents = [SCNAnimationEvent(keyTime: fadeTime, block: chainEventBlock)]
}

关于ios - SceneKit——将 Objective-C block 转换为 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29134036/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com