开发者

Objective-C private instance variables definition

Is there any difference in where we define private instance variables? As I understand there are two possibilties:

1) In header file

开发者_如何学JAVA@interface MyViewController : UIViewController {
    @private
    NSString *fooString;
}

2) Second way is to define it in the implementation:

@implementation MyViewController
NSString *fooString;

What is the difference? Cheers!


In 2nd case fooString is not instance variable - it is global variable, so your two cases are completely different


The first way defines a private instance variable. Each object of class MyViewController will have its own private fooString.

The second way defines a global variable. There will be only one instance of fooString and it will be visible to any source file with the following declaration:

extern NSString *fooString;


The first one is a unique fooString per MyViewController that you create.

The second is a fooString that every MyViewController shares.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜