1

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 Answers 3

3

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.

4
  • 1
    My project's target is iOS 8.0. Commented Feb 18, 2015 at 10:30
  • @TomaszBąk Can you check the "Builds for" settings in File Inspector tab of your storyboard or xib file ? It must be set to iOS 4.3 and later , I think . Else I don't see any other reason as to why Xcode will create unsafe_unretained references . Commented Feb 19, 2015 at 17:59
  • 1
    Please don't copy text from other sources without proper attribution. I've added this attribution above, but make sure to do this yourself in the future. Commented Feb 19, 2015 at 18:32
  • 1
    @bharat-jagtap Sorrry, but Builds for is set to a current development target (iOS 8.0). Commented Feb 20, 2015 at 10:43
-3

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.

1
  • 1
    Answer doesn't address my question - why Xcode's behaviour have changed. Commented Feb 18, 2015 at 10:33
-3

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.

1
  • 1
    I wasn't asking what is strong/weak, but why Xcode's behaviour have changed. Commented Feb 18, 2015 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.