What does "@private" mean in Objective-C?

user100051 picture user100051 · May 10, 2009 · Viewed 54k times · Source

What does @private mean in Objective-C?

Answer

hbw picture hbw · May 10, 2009

It's a visibility modifier—it means that instance variables declared as @private can only be accessed by instances of the same class. Private members cannot be accessed by subclasses or other classes.

For example:

@interface MyClass : NSObject
{
    @private
    int someVar;  // Can only be accessed by instances of MyClass

    @public
    int aPublicVar;  // Can be accessed by any object
}
@end

Also, to clarify, methods are always public in Objective-C. There are ways of "hiding" method declarations, though—see this question for more information.