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
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
-
@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
-
1Please 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
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
.
-
1Answer doesn't address my question - why Xcode's behaviour have changed. Commented Feb 18, 2015 at 10:33
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:
- delegate properties, which are often referenced weakly to avoid retain cycles, and
- 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.
-
1I wasn't asking what is strong/weak, but why Xcode's behaviour have changed. Commented Feb 18, 2015 at 10:31