0

Possible Duplicate:
@property and retain, assign, copy, nonatomic

Can someone provide me overview on the properties: retain, assign, copy, nonatomic. I am the new in iPhone development and I don't know how and when to use these

Thanks in advance.

Community
  • 1
  • 1
Tej
  • 41
  • 1
  • 6
  • 3
    http://stackoverflow.com/questions/2255861/property-and-retain-assign-copy-nonatomic – ichanduu May 08 '12 at 08:20
  • 4
    As you are new to iPhone development, I assume you haven't yet had time to discover the search facility that Apple provide you with. By using this search feature, you can discover such wonderful things as [this memory management document](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html) – Nick Bull May 08 '12 at 08:21
  • @Nick Bull you're nearly right, but should be this one (iOS!): https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html – Kai Huppmann May 08 '12 at 08:24
  • 1
    @Kai It's actually exactly the same document. But well noticed! – Nick Bull May 08 '12 at 08:29
  • U can read about property attributes on [apple.developer](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html). – Feo May 08 '12 at 08:38
  • Of course you should know what that mean, why using it to completely understand memory-managment in iOS. And in comments to your questions you can find very useful urls to start. But if you think on new project targeted in iOS 5 you can use [ARC (Automatic Reference Counting)](https://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html). This is very VERY useful for iOS 5 developers. – Jakub May 08 '12 at 08:48

1 Answers1

4

If you use your own getters/setters then these keywords don't have much of a meaning but if you use @property/@synthesize then u need to use the keywords.

  • Retain: In this case you get and extra object made that is the retain count for that object is increased by 1 for every retain and you need to release it if you are using arc.Retain is used when you don't want the value to be deallocated while it is set on the object.Also retain creates a strong reference,and an object cannot be deallocated until all of its strong references are released.

  • Copy: Copy is just reverse of retain as it just gives a copy of the object to work on and the actual value changed on copied object wont be reflected back on the real object . One should use a copy accessor when the setter parameter may be mutable but you can't have the internal state of a property changing without warning .

  • Assign:Assign is generally used for non-object Datatypes.

  • Non-atomic: Non-atomic offers thread-safety whereas default atomic doesn't but the read/writes of atomic are thread safe it uses an object level lock that ensure serialization of read/writes.Also ,the value returned from the getter or set via the setter is always fully retrieved or set regardless of what other threads are executing concurrently.If you specify strong, copy, or retain and do not specify nonatomic, then in a reference-counted environment, a synthesized get accessor for an object property uses a lock and retains and autoreleases the returned value.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
Abhishek Singh
  • 6,068
  • 1
  • 23
  • 25