2017-04-07 60 views
0

我試圖運行SceneKit動畫片 「DAE」 的模式:SceneKit Swift3編譯器錯誤

let url = Bundle.main.url(forResource: "art.scnassets/Player(walking)", withExtension: "dae") 
    let sceneSource = SCNSceneSource(url: url!, options: [SCNSceneSource.LoadingOption.animationImportPolicy: SCNSceneSource.AnimationImportPolicy.playRepeatedly]) 
    let animationIds: NSArray = sceneSource?.identifiersOfEntries(withClass: CAAnimation) 

    for eachId in animationIds { 
     let animation: CAAnimation = (sceneSource?.entryWithIdentifier(eachId as! String, withClass: CAAnimation.self))! 
     animations.add(animation) 
    } 
    character?._walkAnimations = animations 

但是編譯器它拋出就行了:

讓animationIds:NSArray的= sceneSource? identifiersOfEntries(withClass:CAAnimation)

並寫入錯誤:

不能轉換v類型'[String]?'的含義到指定類型'NSArray'

請幫我解決這個問題。

在此先感謝。

回答

0

爲什麼要轉換[String]?NSArray,然後每個元素再次轉換爲String,用不着它只是使用SWIFT本地Arrayif letguard let包裹的可選值。

guard let animationIds = sceneSource?.identifiersOfEntries(withClass: CAAnimation) else { 
    return 
} 
for eachId in animationIds { 
    if let animation = sceneSource?.entryWithIdentifier(eachId, withClass: CAAnimation.self) { 
     animations.add(animation) 
    } 
} 
character?._walkAnimations = animations 
+0

感謝您的幫助。 –