The following code is what I'm working on to animate characters in a SK scene class. I can run the code without building, no problem, but I can't build it and run it; I get one error message: Binary operator '+=' cannot be applied to operands of type '[(SKTexture)]' and '[SKTexture!}'. The code is below:

func initStoryBegin() {
    let CaitAtlas = SKTextureAtlas(named: "cait")
    let DrewAtlas = SKTextureAtlas(named: "drew")

    CwalkAnimation = self.animateSheet(CaitAtlas, walkAnimation: CwalkAnimation, formattedword: "cait%01d")

// ***This is where I am having trouble. In the walkAnimation += line.
func animateSheet(nameAtlas: SKTextureAtlas!, walkAnimation: [(SKTexture)], formattedword: String) -> [SKTexture] {
    for index in 1...nameAtlas.textureNames.count {
        let imgName = String(format: formattedword, index)
        walkAnimation += [nameAtlas.textureNamed(imgName)]
        return walkAnimation

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    let charNode = self.childNodeWithName("charNode")

    if (charNode != nil) {
        let animation = SKAction.animateWithTextures(CwalkAnimation, timePerFrame: 0.2)



I can't figure out why the code works when I run it, but it doesn't work when I try to build and run? Any help would be greatly appreciated.


Your walkAnimation array needs to be defined as inout in order to be mutated. Arrays in Swift are treated as Structs, which cannot be altered when passed by value, which is the default behavior. Using "inout walkAnimation: [(SKTexture)]" allows the array to be passed by reference, thus allowing a change such as your append.

  • \$\begingroup\$ Thanks! I figured it out a while back, but had not come back to post the answer, but your explanation does the trick, so no need for me to reinvent the wheel. Thank you very much, I'd upvote it, but I don't have the reputation for that at the moment. \$\endgroup\$ – Bennybear Jun 3 '15 at 20:34
  • \$\begingroup\$ You're quite welcome \$\endgroup\$ – Attackfarm Jun 4 '15 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.