How to add properties to NSMutableArray via catego

2019-01-15 15:07发布

I know that I can "subclass" an NSMutableArray using "category extensions," i.e. @interface NSMutableArray (MyExtension), to add new functions to the class. However, is there a way using category extensions to also add new properties to the extension?

2条回答
Explosion°爆炸
2楼-- · 2019-01-15 15:48

Note that @interface NSMutableArray (MyStuff) is a category and not a class extension. They are similar in this context, but actually do have quite a few different details.

You can't add storage to an existing class through either mechanism. You can use Associative References to associate data with an instance, though.

查看更多
We Are One
3楼-- · 2019-01-15 15:48

As you said, categories can add only methods to a class, not ivars. You could declare property and setProperty: methods in the category, but I'm not sure where you'd store the actual data of your pseudo-property.

查看更多
登录 后发表回答