New behaviour for IBOutlet creation in Xcode 6

547 Views Asked by At

I remember that in Xcode 5 if you dragged a view from a storyboard to the code it would create a property with weak attribute. Now in Xcode 6 it uses unsafe_unretained as a default. What may be the cause of that change?

3

There are 3 best solutions below

1
On

weak properties are like unsafe_unretained just that they work a bit smarter. When the object assigned to the property is released, a weak reference automatically becomes nil to avoid crashes when sending messages to that object (its memory address). Unsafe_unretained properties don't do this. They will always hold on to the memory address (unless you manually change it) assigned to it, regardless of the object associated to that address. Weak references can prevent crashes in such a case but the result still won't be as expected. If your code is well written and organized this shouldn't happen.

And no wonder if apple sets outlet properties to unsafe_unretained.

4
On

As stated on Wikipedia:

Zeroing weak references are only available in Mac OS X 10.7 "Lion" or later and iOS 5 or later, because they require additional support from the Objective-C runtime. Code that uses ARC but needs to support versions of the OS older than Mac OS X Lion or iOS 5.0 cannot use zeroing weak references, and therefore must use unsafe_unretained weak references

Your project deployment target iOS should be something prior to iOS 5 ( probably iOS 4.0 ) . So Xcode is creating unsafe_unretained instead of week as your app should work on iOS 4 or prior iOS.

1
On

A strong reference (which you will use in most cases) means that you want to "own" the object you are referencing with this property/variable. The compiler will take care that any object that you assign to this property will not be destroyed as long as you (or any other object) points to it with a strong reference. Only once you set the property to nil will the object get destroyed (unless one or more other objects also hold a strong reference to it).

In contrast, with a weak reference you signify that you don't want to have control over the object's lifetime. The object you are referencing weakly only lives on because at least one other object holds a strong reference to it. Once that is no longer the case, the object gets destroyed and your weak property will automatically get set to nil. The most frequent use cases of weak references in iOS are:

  1. delegate properties, which are often referenced weakly to avoid retain cycles, and
  2. subviews/controls of a view controller's main view because those views are already strongly held by the main view.

unsafe_retained is same as weak whereas retained is same as strong.