What happened to the UIView?() constructor in Swift 3.0?

The following does initialise x to nil, the brackets are entirely superfluous.

var x: UIView?
return x == nil

Will return true

Check out the developer docs for more information.

https://developer.apple.com/library/content//documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html

If you define an optional variable without providing a default value,
the variable is automatically set to nil for you:

var surveyAnswer: String?
// surveyAnswer is automatically set to nil

@Hamish posted a comment to clarify the reason why UIView?() no longer works:

“The syntax UIView? is just syntactic sugar for Optional, therefore UIView?() is just syntactic sugar for Optional.init(). In Swift 2, Optional’s init() constructs a new optional value set to .None. So yes, it was working correctly. In Swift 3 however, this initialiser has been removed, which is why UIView?() no longer works”

Leave a Comment