Swift 2 migration error: no longer can pass in nil in the designated initialiser of SKSpriteNode

Swift 2 migration error: no longer can pass in nil in the designated initialiser of SKSpriteNode

正在尝试将我的项目迁移到 Swift 2.

我收到大量编译器错误,其中大部分很容易修复,因为它们只是语法更改。

但是,我不知道处理这个错误的最佳方法:

从我的 SKSpriteNode 子类调用指定初始化程序时,我收到错误消息:"Type 'UIColor' does not conform to protocol 'NilLiteralConvertible'" for passing nil for the color:

import SpriteKit

class MyClass: SKSpriteNode {

  init() {
    let texture = SKTexture(imageNamed: "MyTexture.png")
    super.init(texture: texture, color: nil, size: texture.size())
  }
}

我可以通过传入 UIColor.clearColor() 来解决这个问题,但我觉得这不是处理这个问题的正确方法。

非常感谢任何建议,还有,这里有什么变化(当然,link 解释就足够了)?

看起来,according to the docs(尽管没有明确说明,这令人困惑),如果您使用颜色进行初始化,则需要传递 nil 以外的实际值.

我得出的结论是您可以仅使用纹理和大小进行初始化:

convenience init(texture: SKTexture?, size: CGSize)

使用它不会默认颜色,这应该会为您提供您正在寻找的结果,而无需 UIColor.clearColor()UIColor 声明。但是,如果您只是使用纹理进行初始化,颜色将默认为白色 (1.0,1.0,1.0) 并且大小将从纹理继承:

convenience init(texture texture: SKTexture?)
//The size property of the sprite is set to the dimensions of the texture. The color property is set to white (1.0,1.0,1.0).

鉴于此,我假设(再一次,你是对的,这真的很令人困惑)你需要传递一些值,无论是 RGB 还是 UIColor

in Swift 1.2 你在 super 上调用的初始化程序曾经接受颜色的隐式解包可选,因此你能够传递 nil

在 Swift 2 中,相同的初始化器不再接受可选的,如果你使用它,你必须传递一个 UIColor(参见 docs

你通过 UIColor.clearColor() 的解决方案对我来说似乎很合理!