Using
self.backgroundColor = .init(named: "my-color")
where self
is an UIView
, triggers this error:
Value of optional type 'UIColor?' must be unwrapped to a value of type 'UIColor'
Coalesce using '??' to provide a default when the optional value contains 'nil' [Fix]
Force-unwrap using '!' to abort execution if the optional value contains 'nil' [Fix]
The compiler knows that the backgroundColor
property is of type UIColor?
, so it should infer it for the initializer, right?
Moreover, knowing that the property is optional, the error message doesn't make too much sense for me.
Also, both auto fixes are "recursive", meaning that they don't fix the error, and I can apply them over and over again.
Note: I know that I can use UIColor.init
or simply UIColor
. This is just a simplified example.
self.backgroundColor
actually is. – IMSoP Jan 13 at 17:29UIColor?
. I ran the code myself and could verify the same result as observed in this question and I still think it's a valid and intriguing question. It seems there's something about the way the swift semantic analizer is trying to infer the types. – Jobert Jan 13 at 17:43self
is anUIView
. But even without this, it's irrelevant, since I said that it's a property of typeUIColor?
. – Iulian Onofrei Jan 13 at 18:10