A. If you want a completely private variable. Do not give him property.
B. If you want the readonly variable, accessible externally from the encapsulation of the class, to use a combination of the global variable and the property:
//Header @interface Class{ NSObject *_aProperty } @property (nonatomic, readonly) NSObject *aProperty; // In the implementation @synthesize aProperty = _aProperty; //Naming convention prefix _ supported 2012 by Apple.
Using the readonly modifier, we can now access the property elsewhere.
Class *c = [[Class alloc]init]; NSObject *obj = c.aProperty;
But internally, we cannot set aProperty inside the class:
// In the implementation self.aProperty = [[NSObject alloc]init]; //Gives Compiler warning. Cannot write to property because of readonly modifier. //Solution: _aProperty = [[NSObject alloc]init]; //Bypass property and access the global variable directly
Ospho Feb 27 '13 at 0:34 2013-02-27 00:34
source share