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

前端之家收集整理的这篇文章主要介绍了如何在Objective-C中将变量添加到类别?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
需要创建一个带有新变量的类别(类型为NSArray).

OriginalClass Extension.h:

@H_404_4@@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帖子.

原文链接:https://www.f2er.com/c/239609.html

猜你在找的C&C++相关文章