将 Swift 1 数组代码更新为 Swift 2 时出错
Error updating Swift 1 array code to Swift 2
你好,我刚刚更新到 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)
已更改为 Swift 中的 SKAction.sequence(Array<SKAction>)
,或 ObjC 中的 SKAction.sequence(NSArray<SKAction*>*)
。
你好,我刚刚更新到 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)
已更改为 Swift 中的 SKAction.sequence(Array<SKAction>)
,或 ObjC 中的 SKAction.sequence(NSArray<SKAction*>*)
。