向 iPhone 中的 UIImage 添加类别

发布于 2024-10-31 11:27:49 字数 597 浏览 5 评论 0原文

我想向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟酉 2024-11-07 11:27:49
[UIImage performStuff]; // Correct call

[UIImageView performStuff]; // It is a UIImage category not UIImageView

适合初学者。

所以你会做这样的事情......

UIImage* image = [UIImage imageNamed:@"test"];
[image performStuff];
[UIImage performStuff]; // Correct call

not

[UIImageView performStuff]; // It is a UIImage category not UIImageView

for starters.

So you would do something like this...

UIImage* image = [UIImage imageNamed:@"test"];
[image performStuff];
囚你心 2024-11-07 11:27:49

如果该类别位于目标项目中,则只需包含 DoingStuff.h。如果在静态库内,链接器不会链接它,因为它不知道应该链接它不知道将被使用的目标c对象。

您需要将 -all_load、-force_load 或 -ObjC 之一放入目标项目的其他链接器选项中。不同的人报告说其中一些或全部使链接器工作。从 XCode 4 -ObjC 开始应该足够了。

也就是说,我对其中任何一个都没有成功,最终将库中的类别添加到目标项目作为参考。

编辑

刚刚看到我的错误,可能它被链接但是是一个实例方法,并且您将其作为类方法调用:

不是

[UIImage performStuff];

但是

UIImage *image = <your image>;
[image performStuff];

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

[UIImage performStuff];

but

UIImage *image = <your image>;
[image performStuff];
浮生未歇 2024-11-07 11:27:49

根据您设置类别的方式,您需要像这样调用它:

UIImage* myImage = [UIImage imageNamed:@"testImage"];
[myImage performStuff];

此外,您没有遵循类别的命名约定。你的类名应该更像“`UIImage+DoingStuff.h”。我建议重构它,以便其他人更清楚它是一个类别。

当然,您需要将头文件导入到要使用它的类中:

#import "DoingStuff.h"

According to how you have your category set up, you would then need to call it like this:

UIImage* myImage = [UIImage imageNamed:@"testImage"];
[myImage performStuff];

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:

#import "DoingStuff.h"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文