Take the 2-minute tour ×

In the modern runtime. you can declare property without having the same name ivar already declared in the interface like

@interface MyClass: NSObject
     NSString *str2;
@property (retain) NSString *str1;
@property (retain) NSString *str2;

And i know ,this would generate a str1 ivar automatically for you since in the implementation, you can access both self.str1 or purely str1.

But my question is that, is there any difference between str1 and str2?

If they are the same, why would class extension (a catagory with no name) allow to add a new declared property that would also generate an ivar but not allowing adding a ivar instance in a class extension?(I know LLVM 2.0 or later allows this but gcc don't)

share|improve this question
add comment

1 Answer

str1 and str2 are the same because when you use @property setter and getter methods are automatically generated, but also created is an instance variable of the same name if there is no previously existing one to store values. For example, in your code, the compiler will say, "It appears that I have to create an instance of class NSString because the programmer wants to access it." But with str2, an instance variable is already created to store data so there is no need for the compiler to create one.

Personally, I code like you did with str1, not declaring the variable in @interface.

Hope this helps.

Also check out this: What's the purpose of an ivar when a property exists?

share|improve this answer
It's not the property directive, but @synthesize that creates the variable: developer.apple.com/library/mac/documentation/Cocoa/Conceptual/… –  Josh Caswell Jun 4 '12 at 4:13
add comment

Your Answer


