2016-05-05 108 views
0

我得到下面的代碼中的錯誤:無法將類型的價值「的NSMutableArray」預期參數類型「[SKTexture]」

func prepareAnimationForDictionary(settings: NSDictionary,repeated: Bool) -> SKAction { 
     let atlas: SKTextureAtlas = 
      SKTextureAtlas(named: settings["AtlasFileName"] as! String) 
     let textureNames:NSArray = settings["Frames"] as! NSArray 
     let texturePack: NSMutableArray = [] 

     for texPath in textureNames { 
      texturePack.addObject(atlas.textureNamed(texPath as! String)) 
     } 

     let timePerFrame: NSTimeInterval = Double(1.0/(settings["FPS"] 
     as! Float)) 

     let anim:SKAction = SKAction.animateWithTextures(texturePack, 
      timePerFrame: timePerFrame) // the error I get is here 
     if repeated { 
     return SKAction.repeatActionForever(anim) 
     }else{ 
     return anim 
     } 
+0

檢查此答案http://stackoverflow.com/questions/25837539/how-can-i-cast-an-nsmutablearray-to-a-swift-array-of-a-specific-type#25837720 –

回答

0

變化timePerFrame(timePerFrame as [AnyObject]) as! [SKTexture]

3

只需使用預期(斯威夫特)類型

... 
let textureNames = settings["Frames"] as! [String] 
var texturePack = [SKTexture]() 

for texPath in textureNames { 
    texturePack.append(atlas.textureNamed(texPath)) 
} 
... 

但從雨燕點可變基金會收藏類型NSMutableArrayNSMutableDictionary是未指定的類型,與Swift本地對應方無關。

0

好的,想想這個。你有一個變量texturePack。您不顯示聲明,但基於錯誤消息,我將假定它是NSMutableArray類型。有問題的電話需要一組SKTexture對象。

所以,投你texturePack到所需的類型:

let anim:SKAction = SKAction.animateWithTextures(texturePack as! [SKTexture], 
    timePerFrame: timePerFrame) //error i get is here 

注意,如果有任何機會,texturePack不是SKTexture對象的數組,你會使用if letguard檢查會更好轉換成功:

guard 
    let anim:SKAction = SKAction.animateWithTextures(texturePack as! [SKTexture], 
    timePerFrame: timePerFrame) //error i get is here 
else 
{ 
    return nil; 
} 

或者,正如其他人所說,改變你的texturePack數組的聲明爲類型[SKTexture]

+0

這是正確,但是'texturePack'不需要'NSMutableArray'而不需要快速'Array' – Alexander

+0

起初我沒有看到OP發佈的代碼中的數組聲明,所以我認爲可能有一個合理的原因數組是一個NSMutableArray而不是一個Swift類型的數組。如果你可以使數組成爲SKTexture對象的Swift數組,它肯定會更好。 (或者你可以使用新類型的NSArray結構。) –

+0

謝謝你們的幫助。非常感激 –

相關問題