0

Is it recommended to use assign property or variables? For example :

@property(assing,nonatomic)int num;

or declare the int variable at the implementation :

@implementaion {
int num;
}
Shevliaskovic
  • 1,522
  • 4
  • 23
  • 41
user3516596
  • 309
  • 4
  • 13

1 Answers1

2

In general, if you want to expose things to other objects (i.e. other view controllers or things that touch this object), you can use "@property".

If you want the data to stay internal to the object, use an ivar.

Obviously my answer is greatly simplified, but @property also comes with extra goodness like ARC (retaining the object or doing atomic vs. non-atomic). In my own code, I'm using @property more and more and ivars less and less.

Community
  • 1
  • 1
Michael Dautermann
  • 86,557
  • 17
  • 155
  • 196
  • +1 I agree that public interfaces should be declared as properties in the .h. But I wouldn't want future readers to infer that if it's private, you wouldn't want to use a property. It's just that you'd define private properties in the class extension in the .m file. I'd suggest that ivars are best reserved for private variables for which you don't need/want all the `@property` "goodness" (KVC, atomic/nonatomic, simplified memory semantics, etc.). Personally, I use properties (both public and private) almost exclusively now, but if one wants to use ivars for private variables, you can do that. – Rob May 03 '14 at 14:34