Strong Feelings About Weak IBOutlets

Peter Wood in Swift
08 Jul 2014, 15:03

UPD: creating strong IBOutlet was added

Hey there.

Today I would like to tell you about one more Swift peculiarity, or to be more precise, flaw. It’s about creating strong IBOutlet.

In Swift, ordinary variables (var let) are by default strong. IBOutlet, on the contrary, is weak. In most cases it works fine until it comes to declaring IBOutlet as strong reference.

Though it happens quite rare, we need strong IBOutlets, in cases like:

  • IBOutlet for NSTextField
  • IBOutlet for top level object in iOS app

That’s when we find out that Swift cannot do this!

Apple devs have already admitted that this is a bug:
“Thanks for the bug report. We’re working on a solution for this.”

Well, thanks! But we need it NOW!

We’ve found a workaround by declaring the IBOutlet:

var strongTextView:NSTextView?
@IBOutlet var textView: NSTextView {
get {
return strongTextView
set {
strongTextView = newValue

Simple but effective.
Thanks for staying with us.

Leave a Reply