Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring private member variables

I've started learning Objective-C a few weeks ago and I still don't understand how to manage the encapsulation of a class correctly. What is the best way to declare a private member variable in a class?

It seems that setting the right getter/setter for your member variable with "@property" is the right way to go, more than just declaring it "@private" in the interface. But it seems to me that this still gives other classes an access to these variables. Even if you declare the property "readonly", an outside class can access the reference to the member variable and modify it!

So I'm guessing the best way to declare a private member variable is to not include any guetter/setter by not declaring a property. Am i right? Or is there a better way?

Thanks

like image 556
Michael Eilers Smith Avatar asked Aug 26 '10 01:08

Michael Eilers Smith


People also ask

Why do we declare variables as private?

Making a variable private "protects" its value when the code runs. At this level, we are not concerned with protecting it from other programmers changing the code itself. The point of so-called "data hiding" is to keep internal data hidden from other classes which use the class.

How do you declare a private 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.

Can we declare variable as private in Java?

Private Access Modifier - Private Class and interfaces cannot be private. Variables that are declared private can be accessed outside the class, if public getter methods are present in the class. Using the private modifier is the main way that an object encapsulates itself and hides data from the outside world.


1 Answers

if you don't want it accessible to other classes, declare the @property on your implementation, creating an anonymous category for your class.

Header file:

// MyClass.h
@interface MyClass : NSObject {
    NSObject *_privateObject;
    NSObject *_readonlyObject;
    NSObject *_publicObject;
}

@property (nonatomic, retain, readonly) NSObject *readonlyObject;
@property (nonatomic, retain) NSObject *publicObject;

@end

Implementation:

// MyClass.m
@interface MyClass ()
    @property (nonatomic, retain) NSObject *privateObject;
    // Make it writable on the implementation
    @property (nonatomic, retain, readwrite) NSObject *readonlyObject;
@end

@implementation MyClass

@synthesize privateObject = _privateObject;
@synthesize readonlyObject = _readonlyObject;
@synthesize publicObject = _publicObject;

These are examples of three different properties.

  • privateObject is not visible on other classes;
  • readonlyObject is visible but is read only;
  • publicObject is visible and can be get and set;
like image 173
vfn Avatar answered Oct 27 '22 02:10

vfn