2015-03-18 61 views
0

有沒有一個Swift guru知道如何將這個代碼在objective-c中轉換成Swift?Swift Guru能夠將此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; 
    } 

在斯威夫特,我想:

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

感謝

回答

1

聲明該塊的返回類型爲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)] 
} 
+0

非常感謝Rengers – Paul 2015-03-18 23:27:00