'UIColor!' is not identical to 'SKColor'

swift

Solution

The problem is that `UIColor` and `UIColor!` are technically different types - the `!` stands for Implicitly Unwrapped Optional and is used where there is an optional value, but that optional should always have a value.

It seems that many objects returned from the system frameworks use implicitly optional types, though this usage should get less common as the frameworks are fully converted to Swift - there's no reason UIColor.redColor() would ever return a nil color, so its return type will probably change from `SKColor!` to `SKColor` in the future.

In your code example, if you change your `let gradient` declaration to explicitly declare the array as being of type `[SKColor]` (as opposed to `SKColor!`) the compiler happily carries on:

let gradient : [SKColor] = [SKColor.redColor(), SKColor.magentaColor()]

Problem

Can someone please explain why if, as it says in the import definition: ``` typealias SKColor = UIColor ``` I get the error 'UIColor!' is not identical to 'SKColor' when I do the following? I was about to say 'I know the difference between UIColor and UIColor!' but actually, maybe I don't truly understand! ``` import UIKit import SpriteKit func nColours(gradient: [SKColor]) -> Int { return gradient.count } let gradient = [SKColor.redColor(), SKColor.magentaColor()] nColours([SKColor.redColor(), SKColor.magentaColor()]) // 2, OK nColours(gradient) // <<<<<< Error // 'UIColor!' is not identical to 'SKColor' ``` experimenting, I tried this : ``` let gradient = [SKColor.redColor(), SKColor.magentaColor()] let b = (gradient == [SKColor.redColor(), SKColor.magentaColor()]) // <<<<<< Error // '[UIColor!]' is not convertible to '_ArrayCastKind' ```

Original source