类扩展和框架
我开发了自己的框架,其中包含我经常在应用程序中使用的有用的类/方法。最近,我为 NSString 添加了一个类扩展“NSString+Extensions.h/m”来添加我自己的方法。示例:
- NSString+Extensions.h
@interface NSString(扩展) - (NSString *)删除变音符号; @结尾
- NSString+Extensions.m
#import "NSString+Extensions.h" @implementation NSString(扩展) - (NSString *)删除变音符号 { 返回 [[[NSString alloc] initWithData:[self dataUsingEncoding:NSASCIIStringEncoding allowedLossyConversion:YES] 编码:NSASCIIStringEncoding] autorelease]; } @结尾
我成功编译了我的框架。但是当我尝试在任何应用程序中使用此类扩展的功能之一时:
- AppDelegate.m
// CUtils 是框架的名称。 CUtils.h 包含所有头文件的 #import // 包含在我的框架中 #import
; @实现AppDelegate ... - (void)applicationDidBecomeActive:(UIApplication *)application { /* 重新启动应用程序处于非活动状态时暂停(或尚未启动)的所有任务。如果应用程序之前在后台运行,则可以选择刷新用户界面。 */ NSString *toto = @"Je suis une chaîne avec des caractères spéciaux"; NSLog(@"%@", toto); NSLog(@"%@", [toto 删除Diacritics]); } ...
我收到以下错误:
2012-01-31 17:01:09.921 TestCUtils[4782:207] Je suis une chaîne avec des caractères spéciaux 2012-01-31 17:01:09.924 TestCUtils[4782:207] -[__NSCFConstantString removeDiacritics]: 无法识别的选择器发送到实例 0x340c
但是如果我直接在应用程序中添加类扩展(在我的应用程序之外)框架),它工作正常......
有什么提示吗?
** 编辑 **
正如你们中的一些人所问的,我在“其他链接器标志”中添加了 -all_load 和 -ObjC 选项,但问题仍然存在。
I have develop my own framework that contains useful classes/methods that I often use in my apps. Recently, I've added a class extension for NSString "NSString+Extensions.h/m" to add my own methods. Example :
- NSString+Extensions.h
@interface NSString (Extensions) - (NSString *)removeDiacritics; @end
- NSString+Extensions.m
#import "NSString+Extensions.h" @implementation NSString (Extensions) - (NSString *)removeDiacritics { return [[[NSString alloc] initWithData:[self dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES] encoding:NSASCIIStringEncoding] autorelease]; } @end
I successfully compile my framework. But when I try to use one of the functions of this class extension in any application :
- AppDelegate.m
// CUtils is the name of the framework. CUtils.h contains #import of all header files // contained in my framework #import <CUtils/CUtils.h> @implementation AppDelegate ... - (void)applicationDidBecomeActive:(UIApplication *)application { /* Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previouslyin the background, optionally refresh the user interface. */ NSString *toto = @"Je suis une chaîne avec des caractères spéciaux"; NSLog(@"%@", toto); NSLog(@"%@", [toto removeDiacritics]); }
...
I get the following error :
2012-01-31 17:01:09.921 TestCUtils[4782:207] Je suis une chaîne avec des caractères spéciaux
2012-01-31 17:01:09.924 TestCUtils[4782:207] -[__NSCFConstantString removeDiacritics]: unrecognized selector sent to instance 0x340c
But if I add my class extension directly in the application (outside of my framework), it works fine...
Any hint?
** EDIT **
As some of you have asked, I've added -all_load and -ObjC options in 'Other Linker Flags', but the issue remains.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看一下这个技术问答,它解释了@Ell Neal 提到的 -ObjC 和 -all_load 选项。
注意 链接器选项需要在链接框架的项目(即框架的客户端)而不是框架本身上设置。从您的屏幕截图来看,您似乎正在框架项目上设置选项,因为我可以在左侧看到源文件
NString+Extensions.m
。Take a look at this technical Q&A that explains the -ObjC and -all_load options that @Ell Neal mentions.
Note The linker options need to be set on the project that is linking the framework (i.e. the client of the framework) not the framework itself. From your screenshot it looks like you are setting the option on your framework project because I can see the source file
NString+Extensions.m
on the left.您需要将
-ObjC
添加到构建设置中的其他链接器标志。如果这不起作用,请尝试添加-all_load
You need to add
-ObjC
to Other Linker Flags in your build settings. If this doesn't work, try adding-all_load
听起来您需要在 AppDelegate.m 中
#import "NSString+Extensions.h"
It sounds like you need to
#import "NSString+Extensions.h"
in your AppDelegate.m