3

Possible Duplicate:
Property Declaration and Automatic Backing Storage Allocation

Why would the compiler accept something like instead of giving an error?

#import <UIKit/UIKit.h>
@interface fubar : NSOjbect {
}
@property (nonatomic, retain) NSArray *array;
@end

Clues appreciated.

Also, an editorial comment... Does this not make the code less readable?

Community
  • 1
  • 1

3 Answers3

3

Compiler can automatically generate ivar for you.

In the implementation file you should find something like this:

@synthesize array;

or

@synthesize array = _array;

I prefer the second case since the ivar name is different from the property name, so you are forced to use

self.array = anArray;

Something like this would not compile:

array = anArray;

Since the ivar name is _array.

If you ask if it's less readable - for me it's not. I prefer automatic ivars generation.

Compare:

#import <UIKit/UIKit.h>
@interface fubar : NSOjbect

@property (nonatomic, retain) NSArray *array;

@end

(I removed curly braces since in this case they are not needed) with this:

#import <UIKit/UIKit.h>
@interface fubar : NSOjbect {
    NSArray *array
}

@property (nonatomic, retain) NSArray *array;

@end

For me the less code the better (easier to understand).

Krokodylowy
  • 610
  • 6
  • 21
1

While the works fine, @synthesize creates the ivar, there is a side effect that the ivar will not be visible in the debugger. Hopefully Apple will address this annoying issue.

zaph
  • 111,848
  • 21
  • 189
  • 228
0

Properties are not instance variables is the simple answer.

Properties are a pair of setter/getter methods which can set/get an instance variable if they like, but don't have to.

If you synthesize the property (i.e. have the compiler automatically create the setter and getter) and an instance variable doesn't exist, the compiler will create that too.

JeremyP
  • 84,577
  • 15
  • 123
  • 161