2016-05-05 68 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

谢谢你们的帮助。非常感激 –

相关问题