In Swift 2.2, I often declared variables using a concise syntax similar to
let x = UIView?()
Cannot invoke initializer for type 'UIView?' with no arguments
Overloads for 'UIView?' exist with these partially matching parameter lists: (Wrapped), (nilLiteral: ())
let x: UIView? = nil
let x = nil as UIView()
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.
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”