3

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?

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

...trying to find a prooflink meanwhile :)
See e.g. here ... "For the modern runtimes [...], instance variables are synthesized as needed. If an instance variable of the same name already exists, it is used."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.