向 iPhone 中的 UIImage 添加类别
我想向 UIImage 添加一个类别并将其链接到我的 iPhone 应用程序,但出现此错误:
(无法识别的选择器)
我的代码: DoingStuff.h
#import <UIKit/UIKit.h>
@interface UIImage (DoingStuff)
- (UIImage *)performStuff;
@end
DoingStuff.m
#import "DoingStuff.h"
@implementation UIImage (DoingStuff)
- (UIImage *)performStuff
{
// My code here
}
@end
但是当我运行我的程序时,我得到这个:
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIImageView PerformStuff:]:无法识别的选择器发送到实例 0x4b415d0”
I want to add a category to UIImage and link it to my iPhone app, but I got this error:
(unrecognized selector)
My code: DoingStuff.h
#import <UIKit/UIKit.h>
@interface UIImage (DoingStuff)
- (UIImage *)performStuff;
@end
DoingStuff.m
#import "DoingStuff.h"
@implementation UIImage (DoingStuff)
- (UIImage *)performStuff
{
// My code here
}
@end
But when I run my program I get this :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView performStuff:]: unrecognized selector sent to instance 0x4b415d0'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不
适合初学者。
所以你会做这样的事情......
not
for starters.
So you would do something like this...
如果该类别位于目标项目中,则只需包含 DoingStuff.h。如果在静态库内,链接器不会链接它,因为它不知道应该链接它不知道将被使用的目标c对象。
您需要将 -all_load、-force_load 或 -ObjC 之一放入目标项目的其他链接器选项中。不同的人报告说其中一些或全部使链接器工作。从 XCode 4 -ObjC 开始应该足够了。
也就是说,我对其中任何一个都没有成功,最终将库中的类别添加到目标项目作为参考。
编辑
刚刚看到我的错误,可能它被链接但是是一个实例方法,并且您将其作为类方法调用:
不是
但是
If the category is in the target project just include the DoingStuff.h. If inside a static library the linker will not link it in, because it doesn't know it should link objective c objects it doesn't know will be used.
You need to put one of -all_load, -force_load or -ObjC in other linker options in you target project. Different people report some or all of them make linker work. Starting with XCode 4 -ObjC should be enough.
That said I didn't have success with any of them and ended in adding the categories from the library to the target project as references.
EDIT
Just saw my mistake, probably it gets linked but is a instance method and you are calling it as a class method:
not
but
根据您设置类别的方式,您需要像这样调用它:
此外,您没有遵循类别的命名约定。你的类名应该更像“`UIImage+DoingStuff.h”。我建议重构它,以便其他人更清楚它是一个类别。
当然,您需要将头文件导入到要使用它的类中:
According to how you have your category set up, you would then need to call it like this:
Additionally, you aren't following the naming conventions for categories. Your class name should be something more like "`UIImage+DoingStuff.h". I would suggest refactoring it so that it's more clear to others that it's a category.
And of course, you need to import the header file into the class you want to use it in: