将Swift 1数组代码更新为Swift 2时出错

时间:2015-12-02 15:09:35

标签: ios arrays swift swift2.1

您好我刚刚更新到swift 2并且我的数组中出现以下错误: 'NSMutableArray'不能隐式转换为'[AnyObject]';你的意思是使用'as'来明确转换吗?

func addAlien(){

    var alien:SKSpriteNode = SKSpriteNode(imageNamed: "cuadradoRojo")
    alien.name = "alien"
    alien.physicsBody = SKPhysicsBody(circleOfRadius: alien.size.width/2)
    alien.physicsBody?.dynamic = true
    var actionArray:NSMutableArray = NSMutableArray()
    var actionArray2:NSMutableArray = NSMutableArray()
    alien.removeFromParent()
    var alienAzul:SKSpriteNode = SKSpriteNode(imageNamed: "cuadradoAzul")
    alienAzul.name = "alien"
    alienAzul.physicsBody = SKPhysicsBody(circleOfRadius: alien.size.width/2)
    alienAzul.physicsBody?.dynamic = true
    alienAzul.removeFromParent()
    if gameOver == false{


        let minX = alien.size.width/2

        let maxX = self.frame.size.width - alien.size.width/2

        let rangeX = maxX - minX
        let position:CGFloat = CGFloat(arc4random()) % CGFloat(rangeX) + CGFloat(minX)

        alien.position = CGPointMake(position, self.frame.size.height + alien.size.height)
        alienAzul.position = CGPointMake(position, self.frame.size.height + alienAzul.size.height)
        var randomCirculo = arc4random_uniform(2)
        if randomCirculo == 0{
            self.addChild(alienAzul)
        }
        else {
            addChild(alien)
        }

        let minDuration = 3


        let duration = Int(minDuration)



        actionArray.addObject(SKAction.moveTo(CGPointMake(position, -alien.size.height), duration: NSTimeInterval(duration)))

        actionArray.addObject(SKAction.removeFromParent())
        actionArray2.addObject(SKAction.moveTo(CGPointMake(position, -alienAzul.size.height), duration: NSTimeInterval(duration)))

        actionArray2.addObject(SKAction.removeFromParent())

        alien.runAction(SKAction.sequence(actionArray as [AnyObject]))
        alienAzul.runAction(SKAction.sequence(actionArray as [AnyObject]))

    }    
}

我把错误放在这里alien.runAction(SKAction.sequence(actionArray as [AnyObject]))alienAzul.runAction(SKAction.sequence(actionArray as [AnyObject]))

1 个答案:

答案 0 :(得分:1)

而不是

var actionArray:NSMutableArray = NSMutableArray()

使用

var actionArray = [SKAction]()

ObjectiveC已更新为支持泛型NSArray,并且大部分框架已相应更新。

SKAction.sequence(NSArray)已在Swift中更改为SKAction.sequence(Array<SKAction>),或在ObjC中更改为SKAction.sequence(NSArray<SKAction*>*)