Reputation: 31
I've recently taken up Swift in order to learn how to program iOS apps. I've been following Skip Wilson's tutorials and there's a moment when he connects an outlet and gets:
@IBOutlet var ticTacImg1: UIImageView = nil
instead of getting that I obtain:
@IBOutlet var ticTacImg1: UIImageView!
if I try to change it Xcode tells me it's an error. What am I doing wrong?
Upvotes: 1
Views: 89
Reputation: 2417
@IBOutlet var ticTacImg1: UIImageView!
is the correct code.
An @IBOutlet
property needs to be optional because it initially has a nil
value before the system connects the view to it. Marking it as an implicitly unwrapped optional with !
means that it will be unwrapped automatically whenever it is used.
I'm not sure which specific tutorial you're referencing, but my initial thought is that he could be using an early beta of Xcode 6 that didn't enforce this.
Upvotes: 1
Reputation: 42598
In early versions of Xcode 6, anything marked as @IBOutlet
was turned into a weak implicitly unwrapped optional.
That is
@IBOutlet var ticTacImg1: UIImageView = nil
would be compiled as
@IBOutlet weak var ticTacImg1: UIImageView! = nil
This is no longer true. Now you must declare its optional status, but it will still be weak.
That is
@IBOutlet var ticTacImg1: UIImageView! = nil
would be compiled as
@IBOutlet weak var ticTacImg1: UIImageView! = nil
The reason why ticTacImg1
is not initialized to nil
is that it doesn't matter. In Swift, implicitly unwrapped optionals which are not initialized have the value nil
, so = nil
is redundant.
Upvotes: 3