开发者

what is @private ?and what its use?

i see in one of my project @private here is code..

@interface mapview : UIViewController<CLLocationManagerDelegate, MKMapViewDelegate,UITextFieldDelegate> {
 @private
  CLLocationManager *_locationManager;
  MKReverseGeocoder *_reverseGeocoder;
  MKMapView* _map开发者_StackOverflow中文版View;
}

what is use of this ?

i know may be this is tomuch low level question ... i want to know use of @private here .


@private limits the scope or "visibility" of the instance variables declared under the @private directive. The compiler (supposedly) enforces this scope and will not allow direct access of private instance variables outside of the class that declares them. In modern Objective-C runtimes (64-bit on OS X or iOS 4 or greater), instance variables do not need to be declared in the class @interface and visibility is not an issue. In legacy runtimes, the instance variables had to be declared in the @interface, so @private was the only way to prevent client code from using the instance variables directly.

See the Objective-C Language Guide for more info (including @public, @protected, and @package visibility modifiers).

GCC doesn't enforce visibility, but I believe Clang 2.0 will.


It means that those instance variables are considered 'private' to the class and should not be accessed directly (which is hardly ever done in Obj-C anyway since it's so dynamic and Cocoa gives you so many free, generated accessors). So, this means that you can't do something like this:

mapview* myMapView = [[mapview alloc] initWithNibName:nil bundle:nil];
CLLocationManager* myMapViewsLocationManager = myMapView->_locationManager; // NO!!

Because the variable is private, the above should not work (note that the compiler actually allows this at the moment, but you get a warning that someday it won't... and I think the clang 2.0 compiler may actually generate a hard error).


@private is a visibility modifier. A variable that is @private can only be seen and used within the class it is defined in.

@public would allow other classes to view and modify this variable.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜