Instance variables are encapsulated by using the private access modifier. Methods can be public or private, but they are usually public.
In actual terms (practically), python doesn't have anything called private member variable in Python. However, adding two underlines(__) at the beginning makes a variable or a method private is the convention used by most python code.
You can use the @private
keyword inside the {}
to make all subsequent variable declarations private. The default visibility is @protected
(which is similar to protected
in Java) and that generally works well. You'd have to specifically declare a variable as @public
for it to be directly accessible outside the class.
This Apple documentation has further details about variable scope and visibility.
There is also a difference between "private API" and private variables. In Objective-C, you cannot make methods private — anyone can call any method. There are several ways to create "secret" methods, but that's somewhat out of the scope of this question. Here are a few related SO questions:
As far as the leading _ in front of variables, be aware that Apple also reserves this prefix for "private" methods. The best way to guarantee you avoid problems is to use normal naming conventions for your own variables and methods. However, unless you subclass something from Cocoa (other than NSObject) you can be fairly confident that you won't run into problems.
With the new LLVM Compiler available in XCode 4 and later, you can declare @private
variables in default categories inside your implementation (.m) file:
@interface ClassName()
{
@private
// private variables here
}
@end
@implementation ClassName
// you can use private variables here
@end
I find this convenient, as I hate the pollution private variables bring into my header files.
You can define private methods by simply having them only in the @implementation, and not the @interface.
Similarly, you can define private instance variables inside an anonymous block at the start of the @implementation - as you do for public ivars inside the @interface.
See the following example.
@interface EXClass : NSObject
{
uint8_t publicInteger;
float publicFloat;
}
-(void)publicMethod;
@end
@implementation EXClass
{
uint8_t privateInteger;
float privatefloat;
}
-(BOOL)privateMethod {
return FALSE;
}
Remember that objective-C methods are sent as messages at runtime, though (rather than C++'s compile time binding), so respondsToSelector: would still return true and performSelector: would still call the method. The ivars would be fully private.
If you were making a library, though, theoretically no one would know about any methods you didn't declare in the header files.
All iVars in Objective-C are protected by default. If you don't write the accessor methods than other classes won't be able to see the variables.
The two exceptions are categories and subclasses.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With