开发者

Is there any difference between the ivar in the class interface and the ones created by declared property(modern runtime)?

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;
@end

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 all开发者_运维问答ows this but gcc don't)


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜