Objective-C类别导入奇怪的行为 [英] Objective-C Category import strange behavior

查看:166
本文介绍了Objective-C类别导入奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从外部库扩展一个类。这是我的代码:

I am extending a class from a external library. Here is my code:

头文件:Manager + MyCategory.h

Header file: Manager+MyCategory.h

#import "Manager.h"
#import "Element.h"

@interface Manager (myCategory) 
- (Element*) elementWithTag:(NSInteger)tag; 
@end

实施文件:Manager + MyCategory.h文件

Implementation file: Manager+MyCategory.h file

@implementation Manager (myCategory) 

- (Element*) elementWithTag:(NSInteger)tag {
    ...
}

@end

现在我是这样的使用此类别:

Now here is how I use this category:

#import "Manager+MyCategory.h"

@implementation myClass 

- (void) myFunction:(NSInteger)tag {
   Manager* myManager = [[Manager alloc] init];
   Element* = [myManager elementWithTag:tag];

   ...

   [myManager release];
}

@end

编译时我没有警告,但是当我运行我的程序时,我收到以下错误:

When I compile I have no warnings but when I run my program I get the following error:

*** -[Manager elementWithTag:]: unrecognized selector sent to instance 0x105d9a0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[Manager elementWithTag:]:

正确导入头文件(即Manager + MyCategory.h),但看起来没有完成与实现文件的链接。
如果我将#importManager + MyCategory **。h **替换为#importManager + MyCategory **。m **,我班上的工作正常。

Header file (ie Manager+MyCategory.h) is correctly imported but it looks like the link with the implementation file is not done. If I replace #import "Manager+MyCategory**.h**" by #import "Manager+MyCategory**.m**" in my class it's working properly.

任何人都可以解释我忘了什么?谢谢

Anyone can explain me what I forgot? Thanks

推荐答案

类别与传统的Unix 静态库不兼容。

Categories do not work well with traditional Unix static libraries.

如果是这种情况,似乎将-ObjC选项传递给链接器可以解决问题。

If that's your case, it seems that passing the -ObjC option to the linker resolves the issue.

Apple Technical Q& A QA1490 解释了这一切。

这篇关于Objective-C类别导入奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆