Objective-C 类别导致无法识别的选择器
我的项目有一个 UIImage
类别函数,我想从另一个类调用它。我正确导入了图像类别的头文件,并且在没有警告的情况下编译了项目。
问题是,当我调用 UIImage 类别函数时,我看到了无法识别的选择器错误,并出现 NSInvalidArgumentException 异常。如果我已经正确链接了所有内容,为什么我会看到这个?
#import <UIKit/UIKit.h>
@interface UIImage (DRShare)
+ (UIImage*) imageNamed:(NSString*)name;
@end
@implementation UIImage (DRShare)
+ (UIImage*) imageNamedDR:(NSString*)name{
CGFloat s = 1.0f;
if([[UIScreen mainScreen] respondsToSelector:@selector(scale)]){
s = [[UIScreen mainScreen] scale];
}
NSString *path = [NSString stringWithFormat:@"%@%@%@.png",kImagesPath,name,s > 1 ? @"@2x":@""];
return [UIImage imageWithContentsOfFile:DRBUNDLE(path)];
}
@end
调用它的文件:
backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamedDR:@"Share Popup Background"]];
引发异常:
2010-10-22 11:51:02.880 Stuff[11432:207] +[UIImage imageNamedDR:]: unrecognized selector sent to class 0x1f8e938
2010-10-22 11:51:02.883 Stuff[11432:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[UIImage imageNamedDR:]: unrecognized selector sent to class 0x1f8e938'
*** Call stack at first throw:
(
0 CoreFoundation 0x02e65b99 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x02fb540e objc_exception_throw + 47
2 CoreFoundation 0x02e6776b +[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x02dd72b6 ___forwarding___ + 966
4 CoreFoundation 0x02dd6e72 _CF_forwarding_prep_0 + 50
5 TapTapShare 0x0001291c -[DRShareViewController backgroundView] + 127
6 TapTapShare 0x00012343 -[DRShareViewController loadView] + 639
7 UIKit 0x0044f54f -[UIViewController view] + 56
8 UIKit 0x0044d9f4 -[UIViewController contentScrollView] + 42
9 UIKit 0x0045d7e2 -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] + 48
10 UIKit 0x0045bea3 -[UINavigationController _layoutViewController:] + 43
11 UIKit 0x0045d12d -[UINavigationController _startTransition:fromViewController:toViewController:] + 524
12 UIKit 0x00457ccd -[UINavigationController _startDeferredTransitionIfNeeded] + 266
13 UIKit 0x00574b55 -[UILayoutContainerView layoutSubviews] + 226
14 QuartzCore 0x02616481 -[CALayer layoutSublayers] + 177
15 QuartzCore 0x026161b1 CALayerLayoutIfNeeded + 220
16 QuartzCore 0x026160bd -[CALayer layoutIfNeeded] + 111
My project has a UIImage
category function that I want to call from another class. I properly import the header file for the image category and I get the project to compile with no warning.
The problem is that when I call the UIImage
category function I seen an unrecognized selector error with a NSInvalidArgumentException
. Why am I seeing this if I've properly linked everything?
#import <UIKit/UIKit.h>
@interface UIImage (DRShare)
+ (UIImage*) imageNamed:(NSString*)name;
@end
@implementation UIImage (DRShare)
+ (UIImage*) imageNamedDR:(NSString*)name{
CGFloat s = 1.0f;
if([[UIScreen mainScreen] respondsToSelector:@selector(scale)]){
s = [[UIScreen mainScreen] scale];
}
NSString *path = [NSString stringWithFormat:@"%@%@%@.png",kImagesPath,name,s > 1 ? @"@2x":@""];
return [UIImage imageWithContentsOfFile:DRBUNDLE(path)];
}
@end
file that calls it:
backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamedDR:@"Share Popup Background"]];
exception raised:
2010-10-22 11:51:02.880 Stuff[11432:207] +[UIImage imageNamedDR:]: unrecognized selector sent to class 0x1f8e938
2010-10-22 11:51:02.883 Stuff[11432:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[UIImage imageNamedDR:]: unrecognized selector sent to class 0x1f8e938'
*** Call stack at first throw:
(
0 CoreFoundation 0x02e65b99 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x02fb540e objc_exception_throw + 47
2 CoreFoundation 0x02e6776b +[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x02dd72b6 ___forwarding___ + 966
4 CoreFoundation 0x02dd6e72 _CF_forwarding_prep_0 + 50
5 TapTapShare 0x0001291c -[DRShareViewController backgroundView] + 127
6 TapTapShare 0x00012343 -[DRShareViewController loadView] + 639
7 UIKit 0x0044f54f -[UIViewController view] + 56
8 UIKit 0x0044d9f4 -[UIViewController contentScrollView] + 42
9 UIKit 0x0045d7e2 -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] + 48
10 UIKit 0x0045bea3 -[UINavigationController _layoutViewController:] + 43
11 UIKit 0x0045d12d -[UINavigationController _startTransition:fromViewController:toViewController:] + 524
12 UIKit 0x00457ccd -[UINavigationController _startDeferredTransitionIfNeeded] + 266
13 UIKit 0x00574b55 -[UILayoutContainerView layoutSubviews] + 226
14 QuartzCore 0x02616481 -[CALayer layoutSublayers] + 177
15 QuartzCore 0x026161b1 CALayerLayoutIfNeeded + 220
16 QuartzCore 0x026160bd -[CALayer layoutIfNeeded] + 111
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
有几种可能性:
UIImage+TTShare.m
链接到您的目标。因此,当您拥有标头时,您并没有编译实现。-all_load
添加到链接到该库的应用的其他链接器标志构建设置。A couple possibilities:
UIImage+TTShare.m
into your target. So while you have the header, you're not compiling the implementation.-all_load
to the Other Linker Flags build setting for the app linking against the library.如果你想使用Category方法,你必须将-ObjC添加到你的APP的Other Linker Flags构建设置中。
If you want to use Category method, you must add -ObjC to the Other Linker Flags build setting of your APP.
我遇到了同样的问题,也必须应用此修复程序。
我的 NSDate-Extensions.m 源文件未编译,因此我必须进入项目设置,然后选择适当的目标,然后单击“构建阶段”选项卡,然后展开“编译源”项目,然后单击 + 符号并手动添加我的 NSDate-Extensions.m 文件。
I had the same issue and had to apply this fix as well.
My NSDate-Extensions.m source file wasn't compiling so I had to go into Project Settings, then select the appropriate target, then click the "Build Phases" tab, then expand the "Compile Sources" items, then click the + symbol and manually add my NSDate-Extensions.m file.
我收到此错误消息,并且我正在使用 Cocoapods。要修复该错误,我只需要再次调用 pod install 即可正确创建所有必要的链接。
I had this error message and I am using Cocoapods. To fix the error, I just needed to call
pod install
again to create all the necessary linking correctly.另一种可能性。
您有类别的实现,但没有接口。
我的意思是你忘记在 *.h 中声明你的类别的接口。
Another possibility.
You have the implementation of category but do not have an interface.
I mean you forgot to declare in *.h the interface of your category.
还有另一种可能性:
承认这一点太尴尬了,但以防万一有人犯了同样的愚蠢错误:
我将代码从一个项目复制到另一个项目,并且错误地在
中粘贴了相同的源代码.h
文件和.m
文件(在这两个文件中,我都放入了.h
文件的代码)。我修复了我的.m
文件并且它起作用了。Yet another possibility:
This is almost too embarrassing to admit, but just in case someone might have done the same silly mistake:
I was copying code from one project to another, and by mistake I had pasted the same source code in the
.h
file and the.m
file (in both I had put the code meant for the.h
file). I fixed my.m
file and it worked.可能是因为你在界面中写的是
imageNamed
而不是imageNamedDR
。May be because you write
imageNamed
instead ofimageNamedDR
in the interface..基于oc创建的sdk混合项目在sdk相关混合demo项目中的swift文件调用时会崩溃,提示找不到sdk中的oc分类方法。我尝试将编译后的包放入带有库的链接二进制文件中(使用库构建阶段链接二进制文件),它有效!仅仅设置demo的依赖来关联SDK是不够的。算是混合sdk和混合demo工程的一个坑吧!
The sdk hybrid project created on the basis of oc will crash when it is called by the swift file in the sdk-related hybrid demo project, prompting that the oc classification method in the sdk cannot be found. I try to put the compiled package in the link binary with libraries(build phase-link binary with libraries), it works! It is not enough to just set the dependencies of the demo to associate the SDK. It can be regarded as a pit of mixed sdk and mixed demo project!