what is diff. b/w @property (nonatomic,assign) and @property (nonatomic,retain)

2 Solutions Collect From Internet About “what is diff. b/w @property (nonatomic,assign) and @property (nonatomic,retain)”

You should assign (= no change in retain count) delegates because you want to avoid “retain loops” (can’t think of a better word)

Take a UITableView and a UIViewController.

When you add the UITableView to your viewController you retain it. Then you assign a delegate and a datasource (which is a delegate too) to your UITableView. Usually this is your viewController.

If the tableview would retain the datasource (your viewController) there would be a “retain loop”

viewontroller retains tableview.
tableview retains viewcontroller

The viewcontrollers dealloc (where you release the tableview) would never be called because tableview would never release your viewcontroller. And the other way around.

And because of this neither would get deallocated. That’s why UITableView only assigns the datasource and the delegate. And you should do the same in your classes.

The meanings of these keywords are related to the memory management implemented by the property:

  • retain: the object will retain a new value set for its property, and will release old values
  • copy: the object will take a copy of the new value, and will release the old value
  • assign: the object will not do any memory management of its property value.

See “setter semantics” in the Objective-C language documentation.

In an automatic reference counting scenario, the following memory management keywords are used:

  • strong: the object keeps a strong (i.e. owning) reference to its property
  • weak: the object keeps a zeroing weak reference to its property