views:

106

answers:

1

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

+4  A: 

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;
vfn
Ok, so using a category I can totally hide a member variable. But I still don't get the point of using "readonly" for a visible member, since it returns the reference to the object. One can just modify the reference and write over it, even if it has no setter.
omegatai
Good point! I'll try to investigate what kind of protection objective-c give to it. My guess is that as soon as you attempt to change the value of your referenced object it make a copy and the changes don't propagate to the protected object. Here it is the documentation: http://developer.apple.com/mac/library/documentation/cocoa/conceptual/objectivec/Articles/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW19
vfn