Swift2のUIColorではnilが渡せない

Swift1.2->Swift2

Xcode7.0.1

  • 起動と同時にConvertしますか?
  • Convertをお願いすると、Diffの画面表示

上記手順であっという間にConvert完了

一部Errorは残る

  • SpriteKit SKTextureでのnilの扱いでエラー発生

    Nil is not compatible with expected argument type 'UIColor'

  • Swift2からはinitilizerでoptionalを許容しないようなコメントあり

stackoverflow


in Swift 1.2 the initializer you are calling on super used to accept an implicitly-unwrapped optional for the color, hence you were able to pass nil

in Swift 2, that same initializer no longer accepts an optional, you must pass a UIColor if you use it (see docs)

your solution of passing UIColor.clearColor() seems reasonable to me!

という事で

let texture = SKTexture(imageNamed: "tooth_normal")
super.init(texture: texture, color: nil, size: texture.size())

↓↓

let texture = SKTexture(imageNamed: "tooth_normal")
super.init(texture: texture, color: UIColor.clearColor(), size: texture.size())

でOK

Swift2からはcolor : nil がだめってのはわかったが、color: UIColor.clearColor()がなぜ初期値として代入出来るんだろ。

ちなみにUIColor.clearColor()は透明の値

Swift1.2まではcolor:nilを渡していたけど、Swift2からはinitでoptionalを渡せないので、何らかの値が必要。という事で、透明(UIColor.clearColor)を渡しておく。という事かな。

このあたりは理解が足りなすぎる。