Search code examples
iosobjective-ccocoaprivate

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


What does @private mean in Objective-C?


Solution

  • 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.