gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:25:43 26 4
gpt4 key购买 nike

你好,我刚刚更新到 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]))

最佳答案

代替

var actionArray:NSMutableArray = NSMutableArray()

使用

var actionArray = [SKAction]()

ObjectiveC 已更新为支持泛型 NSArray,大多数框架也已相应更新。

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

关于ios - 将 Swift 1 数组代码更新为 Swift 2 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34046110/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com