8

i'm a little confused about these two qualifiers... With ARC instead of using weak (i.e. if I need support iOS 4) I can use unsafe_unretained losing the auto-nil features... the final result seems to be similar to assign.

  • Can I exchange unsafe_unretained with assign ?
  • Are these qualifiers the same thing ?

It would be really interesting any link of Apple documentation on this argument... I can find only a few rows here

MatterGoal
  • 16,038
  • 19
  • 109
  • 186
  • 1
    Very nice, clear explanation in this answer to a similar question: http://stackoverflow.com/a/9784807/686385 – big_m Mar 28 '14 at 18:44

1 Answers1

16

Clang's technical specification of ARC goes into much more detail about how the qualifiers work.

But, to answer your question: assign and __unsafe_unretained are not the same thing. assign is a property attribute that tells the compiler how to synthesise the property's setter implementation, while __unsafe_unretained is an ownership qualifier that tells ARC how to insert retain/release calls. But they are related: when declaring a property, assign implies __unsafe_unretained ownership.

Prior to ARC, assign was the default property ownership qualifier; but with ARC enabled, the default for retainable object pointer types is strong. (For scalars and other pointer types, assign is still the default.)

一二三
  • 21,059
  • 11
  • 65
  • 74
  • thus we don't use `unsafe_unretained` as parameter for properties, we use instead `assign` ? – MatterGoal Dec 06 '11 at 14:31
  • It's not that you don't use it, but that you don't need to if you use `assign`. – 一二三 Dec 06 '11 at 23:03
  • so if we cannot use `weak` in order to support iOS 4.3, should we use `unsafe_unretained` or `assign`? – nonopolarity Sep 08 '12 at 07:19
  • 1
    Yes, for iOS 4.3, you should use `unsafe_unretained` or `assign` since `weak` isn't available. Just be conscious that any pointer with these lifetime qualifiers will not be automatically zeroed after a deallocation (thus there is the possibility they may become a dangling pointer). Also, and I believe this may have changed since the original answer was written, but the default qualifier for an object property in ARC is `strong` not `assign` – J Shapiro Dec 09 '12 at 13:47
  • 5
    I think this response misses the original poster's question, which was about the difference between the `assign` and `unsafe_unretained` (no double-underscore prefix) property attributes. According to [section 4.1](http://clang.llvm.org/docs/AutomaticReferenceCounting.html#property-declarations) of the document linked to above, these two attributes both map to `__unsafe_unretained` (with the underscores) ownership, and do actually appear to be equivalent. I think the only difference is that, under ARC, one should use `assign` for non-pointers, and `unsafe_unretained` for pointer types. – big_m Mar 28 '14 at 18:22
  • 2
    You don't really see unsafe_unretained in property declarations. It's either weak/strong or assign/strong for non-ARC. The only time I have ever used __unsafe_unretained (with 2 underscores) is to keep object pointers in a struct: `Struct s { __unsafe_unretained id obj ; } ;` – nielsbot Mar 29 '14 at 23:25
  • 5
    The purpose of unsafe_unretained *as a property attribute* is to provide a scarier looking synonym for assign. The logic is that if it is unsafe, it should look unsafe. – Catfish_Man Mar 30 '14 at 04:14
  • 1
    I like "scarier looking synonym" --- very apt description. :-) The point, of course, is call attention to the extra care required around the use of the property. – big_m Mar 30 '14 at 15:48
  • @big_m is absolutely right in pointing out the correct response to what the question is asking. – John Doe May 09 '16 at 15:23