Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS property declaration clarification

Tags:

This is a two part question in hopes that I can understand more about the topic.

1) It seems to me that you have two popular options for declaring a property for a class in objective c. One is to add the property to the header's class body eg.

@interface MyClass : NSObject {     NSArray *myArray; } 

Or you can add it after the @interface body and before the @end statement like so.

@interface MyClass : NSObject {     // }  @property (nonatomic, retain) NSArray *myArray; 

What is the difference between these two "styles" and when do you choose one over the other?

2) after the @property you find options such as (nonatomic, retain). What are those for and why/when do you use different options?

like image 545
Jacksonkr Avatar asked Feb 06 '12 15:02

Jacksonkr


1 Answers

Here are the only property modifiers that Xcode recognizes:

  • nonatomic (does not enforce thread safety on the property, mainly for use when only one thread shall be used throughout a program)
  • atomic (enforces thread safety on the property, mainly for use when multiple threads shall be used throughout a program) (default)
  • retain / strong (automatically retains / releases values on set, makes sure values do not deallocate unexpectedly) (default if ARC and object type)
  • readonly (cannot set property)
  • readwrite (can both set and get property) (default)
  • assign / unsafe_unretained (no memory management shall be done with this property, it is handled manually by the person assigning the value) (default if not ARC or object type)
  • copy (copies the object before setting it, in cases where the value set must not change due to external factors (strings, arrays, etc).
  • weak (automatically zeroes the reference should the object be deallocated, and does not retain the value passed in)
  • getter=method (sets the selector used for getting the value of this property)
  • setter= method (set the selector used for setting the value of this property)
like image 184
Richard J. Ross III Avatar answered Oct 04 '22 00:10

Richard J. Ross III