6

In iOS 5, "retain" and "release" are not supported any more. Instead "strong" and "weak" are the new way.

iOS 4 code:

@property(nonatomic, retain)
@property(nonatomic, assign)

iOS 5 code:

???
???
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
gsach
  • 5,715
  • 7
  • 27
  • 42

3 Answers3

12

"In iOS 5, retain release are not supported any more." They are, just not when using ARC.

When using ARC, -[<NSObject> retain] is a no-op.

For properties, you can use strong if using ARC but that's not required (you can use retain too if you like). strong and retain are identical:

@property(nonatomic, strong)
@property(nonatomic, assign)

Just make sure you are consistent (don't use both strong and retain in the same project).

  • BTW the Objective-C team did this to be backwards compatible with old code. –  Dec 08 '11 at 18:55
  • Any serious reasons for not using both strong/retain together? Is it just a readability concern? – Craig Otis Dec 08 '11 at 19:03
  • @craig basically. I think it can be confusing to people new to ARC. –  Dec 08 '11 at 19:10
  • 5
    "When using ARC, -[ retain] is a no-op." It's a compile-time error to send a `-retain` message, and it's not really a no-op since the compiler inserts these retains and release for you. Also, `assign` has been replaced by `unsafe_unretained` and `weak`. `weak` is recommended for applications targeting iOS 5.0+. You can use `unsafe_unretained` for older applications, but as its name suggests it is not as safe as the automatically `nil`-ing weak properties. – Brad Larson Dec 08 '11 at 22:22
5

They are not exactly the same but basically retain := strong and assign := weak I would suggest taking a look at the ARC Release notes

Community
  • 1
  • 1
Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
4

nonatomic property states that the object is not thread safe which means if a different thread tries to access this object than bad things can happen but this is much faster than atomic property.

strong is used with ARC and it basically helps you , by not having to worry about the retain count of an object. ARC automatically releases it for you when you are done with it.Using the keyword strong means that you own the object.

weak ownership means that you don't own it and it just keeps track of the object till the object it was assigned to stays , as soon as the second object is released it loses is value. For eg. obj.a=objectB; is used and a has weak property , than its value will only be valid till objectB remains in memory.

copy property is very well explained here https://stackoverflow.com/a/5002646/919545

strong,weak,retain,copy,assign are mutually exclusive so you can't use them on one single object... read the "Declared Properties " section of http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW1

hoping this helps you out a bit...

Community
  • 1
  • 1
Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115