Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

private property in Objective C

Tags:

objective-c

Is there a way to declare a private property in Objective C? The goal is to benefit from synthesized getters and setters implementing a certain memory management scheme, yet not exposed to public.

An attempt to declare a property within a category leads to an error:

@interface MyClass : NSObject {     NSArray *_someArray; }  ...  @end  @interface MyClass (private)  @property (nonatomic, retain) NSArray   *someArray;  @end  @implementation MyClass (private)  @synthesize someArray = _someArray; // ^^^ error here: @synthesize not allowed in a category's implementation  @end  @implementation MyClass  ...  @end 
like image 903
Yurie Avatar asked Apr 13 '11 00:04

Yurie


People also ask

What is a property in Objective-C?

Objective-C properties offer a way to define the information that a class is intended to encapsulate. As you saw in Properties Control Access to an Object's Values, property declarations are included in the interface for a class, like this: @interface XYZPerson : NSObject.

What is Ivar Objective-C?

Accessing an ivar through a getter/setting involves an Objective-C method call, which is much slower (at least 3-4 times) than a "normal" C function call and even a normal C function call would already be multiple times slower than accessing a struct member.

What is synthesize in Objective-C?

@synthesize tells the compiler to take care of the accessor methods creation i.e it will generate the methods based on property description. It will also generate an instance variable to be used which you can specify as above, as a convention it starts with _(underscore)+propertyName.


1 Answers

I implement my private properties like this.

MyClass.m

@interface MyClass ()  @property (nonatomic, retain) NSArray *someArray;  @end  @implementation MyClass  @synthesize someArray;  ... 

That's all you need.

like image 114
Mark Adams Avatar answered Sep 22 '22 18:09

Mark Adams