如何在Objective-C中将变量添加到类别?

需要创建一个带有新变量的类别(类型为NSArray).

OriginalClass Extension.h:

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

@property (nonatomic,retain) NSArray *array;

@end

但是我得到了错误:无法在@interface或@protocol中声明变量.

请帮忙解决问题.

解决方法

正如另一方所说,你不能.虽然有H2CO3指出,你可以使用 associative references.在 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.

如果你想去关联对象,你可以使用this答案.此外,您可以使用Ole Begemann的this帖子.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...