The Joys of Beta Swift: A Change to Outlets

I went to open the pizza demo to get ready for my next post and was greeted by 10 compiler error messages, all the same.

'IBOutlet' property has non-optional type 'UILabel'

Just after I finished the optionals post, It turns out there a change in Beta 4. Before Beta 4, and as described in Apple’s iBook Using Swift with Cocoa and Objective-C.

When you declare an outlet in Swift, the compiler automatically converts the type to a weak implicitly unwrapped optional and assigns it an initial value of nil. In effect, the compiler replaces @IBOutlet var name: Type with @IBOutlet weak var name: Type! = nil.

When I began to write the last post, and every post before then, this was valid code:

 @IBOutlet var priceLabel : UILabel

and Xcode would change this to

 @IBOutlet weak var priceLabel : UILabel! = nil

However, if you control drag an outlet for a label in beta 4 this happens:

@IBOutlet var priceLabel : UILabel!

It’s an understandable change, and does make for clarity. UILabels are optionals, and should be marked as such. So every outlet you enter into code now needs to be declared optional, Xcode is no longer doing it for you, unless you control drag the outlet.

I’ll go back and fix code for previous lessons as soon as I can, but that is what going on if you get ten or more error messages on some of my previous code.

Oh, and one other, small thing:

The range operator was changed too. The .. is now ..< Xcode will suggest that change for you when you load code.

Update August 4, 2014 — I changed all the github code to the correct versions. I now have to go back and do the same for the blogs themselves.

One thought on “The Joys of Beta Swift: A Change to Outlets”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s