0

Possible Duplicate:
iOS: must every iVar really be property?

I really would appreciate if somebody could explain to me why some properties are defined in the interface statement and some as @property ones.

@interface PlacesParser : NSObject
{
    NSMutableArray *arrPlaces;   

    TBXML *tbxml;
}

@property(nonatomic,retain) NSMutableArray *arrPlaces;

-(void)loadRecords:(NSString *)records;
-(void)traverseElement:(TBXMLElement *)element;

@end

In my example we got "arrPlaces" as @property

and "arrPlaces" within the interface.

Community
  • 1
  • 1
mogio
  • 843
  • 2
  • 7
  • 18

2 Answers2

3

The thing in the interface is not actually a property, it is just a plain old instance variable. The @property statement is what makes it a property (giving you the setters and getters). You used to need both, but you don't anymore. If you want your field to be a property with those generated methods, you can leave out the initial field declaration.

Dima
  • 23,484
  • 6
  • 56
  • 83
  • Thanks Dima ... So why and when would I then define properties within the @interface {...}? – mogio Jun 28 '12 at 18:34
  • 1
    @mogio: You can't define properties in the `@interface {}` block. Only instance variables. Properties go after the braces and do not actually define storage. – Chuck Jun 28 '12 at 18:36
  • again, those are NOT properties, they are just ivars. You would declare an ivar without making it a property if you don't want generated accessors/mutators (for example if perhaps it's only getting set/used once so you don't want the overhead). You will probably just want to make most things properties. [More info here](http://stackoverflow.com/questions/719788/property-vs-instance-variable). – Dima Jun 28 '12 at 18:38
  • 1
    ok got it. Everything within the interface are just variables that exist for the life time of the object. And references that are declared with @property are actual property methods (Setter/Getter). Public values that can be changes or read by external objects. – mogio Jun 28 '12 at 18:42
0

What is the difference between ivars and properties in Objective-C

This thread really opened my eyes. Great explonation to my question.

Community
  • 1
  • 1
mogio
  • 843
  • 2
  • 7
  • 18