Bruno Berisso
Bruno Berisso

Reputation: 1091

Properties and instance variable declaration

I notice that you can 'double declare' a variable in this way:

@interface A {
    NSString *instanceVariable;
}
@property (nonatomic, retain) NSString *instanceVariable;
@end

This has the same effect that just do:

@interface A {
}
@property (nonatomic, retain) NSString *instanceVariable;
@end

Why doesn't the compiler complain in situations like this?

Upvotes: 3

Views: 373

Answers (1)

Vladimir
Vladimir

Reputation: 170839

Because both ways are valid.

Declaring ivar via just declaring a property for it is a new language feature available starting objc 2.0

In "Run-time differences" section of "Objective-c programming language" reference stated:

For @synthesize to work in the legacy runtime, you must either provide an instance variable with the same name and compatible type of the property or specify another existing instance variable in the @synthesize statement. With the modern runtime, if you do not provide an instance variable, the compiler adds one for you.

Upvotes: 7

Related Questions