Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a variable to category in Objective-C? [closed]

It's requied to create a category with a new variable (of type NSArray).

OriginalClass+Extension.h:

@interface OriginalClass (Extension) {
    NSArray *_array;
}

@property (nonatomic, retain) NSArray *array;

@end

But I got the error: Cannot declare variable inside @interface or @protocol.

Please help to solve the problem.

like image 276
Dmitry Avatar asked Oct 21 '12 18:10

Dmitry


1 Answers

As the other stated, you can't. Although has H2CO3 pointed out, you can use associative references. On Apple Documents:

Note that a category can’t declare additional instance variables for the class; it includes only methods. However, all instance variables within the scope of the class are also within the scope of the category. That includes all instance variables declared by the class, even ones declared @private.

If you want to go for associated object, you can use this answer. Moreover, you can use this post by Ole Begemann.

like image 148
Rui Peres Avatar answered Sep 22 '22 12:09

Rui Peres