Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a real private instance variable?

People also ask

Can you make instance variables private?

Instance variables are encapsulated by using the private access modifier. Methods can be public or private, but they are usually public.

How do I create a private instance variable in Python?

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:

  • About private instance variables in Objective-C
  • What does “@private” mean in Objective-C?
  • Is it possible to declare a method as private in Objective-C?
  • Best way to define private methods for a class in Objective-C

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.