0

Possible Duplicate:
Are @property(nonatomic)ivar @property(nonatomic,assign)ivar the same or different?

i have seen in many classes that uses delegate which use this keyword assign instead of retain.

y we have use assign instead retain.

may i use assign to myclass ivar.

if i did like that then what is the meaning of it?

Community
  • 1
  • 1
rithik
  • 778
  • 1
  • 9
  • 21

2 Answers2

6

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
6

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.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
  • hey fluch awesome.so the tableview get the datasource and delegate from other classes(from viewcontroller).the viewcontroller retains tableview. – rithik Mar 18 '11 at 14:04
  • if the tableview's datasource and delegate property specified as retain then the tableview retain viewcontroller.so each other get retained .then if viewcontroller done with tableview it will release tableview.but tableview has retain of viewcontroller.since tableview's property delegate retains viewcontroller.so viewcontroller and table view never get released and alone from IOS.right.so we have to refer the viewcontroller as delegate property of tableview with weak referance.that is assign.am i right please reply for my understanding....thanku – rithik Mar 18 '11 at 14:04
  • hey punkt please reply am i right or wrong – rithik Mar 18 '11 at 14:13
  • 1
    usually the "done with tableview, so release it" part happens in the dealloc method of the viewcontroller. but this method is never called because the tableview still retains (at least in our example) the viewcontroller. But the rest is correct. Delegates should have a weak reference created with assign. – Matthias Bauch Mar 18 '11 at 14:27
  • Sorry if my answers sometimes take a while, but I have work to do that doesn't involve gaining rep on SO ^^ – Matthias Bauch Mar 18 '11 at 14:28
  • Thanku Buddy Thanku Buddy Thanku Buddy Thanku Buddy Thanku Buddy Thanku Buddy Thanku Buddy Thanku Buddy Thanku Buddy Thanku Buddy Thanku Buddy Thanku Buddy Thanku Buddy – rithik Mar 18 '11 at 14:39