Owen Abucheri
Owen Abucheri

Reputation: 9

Cannot convert value of type 'NSMutableArray' to expected argument type '[SKTexture]'

I get an error with the following code:

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
        }

Upvotes: 0

Views: 2529

Answers (3)

vadian
vadian

Reputation: 285039

Just use the expected (Swift) types

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

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

From the Swift point of view the mutable Foundation collection types NSMutableArray and NSMutableDictionary are type unspecified and are not related to the Swift native counterparts.

Upvotes: 3

Duncan C
Duncan C

Reputation: 131398

Ok, think about this. You have a variable texturePack. You don't show the declaration, but based on the error message, I'm going to assume it's of type NSMutableArray. The call in question wants an array of SKTexture objects.

So, cast your texturePack to the required type:

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

Note that if there is any chance that texturePack is not an array of SKTexture objects, you would be better off using if let or guard to check that the cast succeeds:

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

Or, as the others have suggested, change the declaration of your texturePack array to be of type [SKTexture]

Upvotes: 0

Vatsal
Vatsal

Reputation: 18171

Change timePerFrame to (timePerFrame as [AnyObject]) as! [SKTexture]

Upvotes: 0

Related Questions