Objective C: how to inherit category methods loaded in a parent class?

只愿长相守 提交于 2019-12-12 21:45:47

问题


In my project, I am using the NSObject+Properties.h category (see aqtoolkit: https://github.com/AlanQuatermain/aqtoolkit/blob/master/Extensions/NSObject%2BProperties.h)

I then have a class that I declare as

#import <Foundation/Foundation.h>
#import "NSObject+Properties.h"

@interface GFDictionaryInitiable : NSObject
...

I then have several subclasses of GFDictionaryInitiable, such as GFRestaurant:

#import <Foundation/Foundation.h>

#import "GFDictionaryInitiable.h"

@interface GFRestaurant : GFDictionaryInitiable
...

My problem is that I cannot access methods defined in the NSObject+Properties category in my subclasses. For example I get the error:

-[GFRestaurant hasPropertyNamed:]: unrecognized selector sent to instance 0x2e57240

How can I have all my subclasses 'inherit' the category's methods that are loaded in the parent class?


回答1:


As you get a runtime exception, and not a compiler error, I assume that you forgot to add "NSObject+Properties.m" to your target. Select the file and check the "Target Membership" checkbox in the File inspector.




回答2:


Just add:

#import "NSObject+Properties.h"

to your sub classes .h files.



来源:https://stackoverflow.com/questions/19432324/objective-c-how-to-inherit-category-methods-loaded-in-a-parent-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!