Objective-C类导致未识别的选择器。

时间:2023-01-15 18:25:13

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.

我的项目有一个UIImage category函数我想从另一个类调用它。我正确地导入图像类别的头文件,我让项目在没有任何警告的情况下编译。

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?

问题是,当我调用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

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

7 个解决方案

#1


202  

A couple possibilities:

两个可能性:

  1. You did not link UIImage+TTShare.m into your target. So while you have the header, you're not compiling the implementation.
  2. 你没有链接UIImage+TTShare。为你的目标。当你有了头,你没有编译实现。
  3. If this is part of a static library, you need to add -all_load to the Other Linker Flags build setting for the app linking against the library.
  4. 如果这是静态库的一部分,那么您需要将-all_load添加到另一个链接器标记为连接库的应用程序构建设置。

#2


6  

If you want to use Category method, you must add -ObjC to the Other Linker Flags build setting of your APP.

如果要使用Category方法,必须向应用程序的其他链接器标记构建设置添加-ObjC。

#3


5  

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.

我也遇到了同样的问题,必须应用这个修复。我的NSDate-Extensions。m源文件没有编译,所以我必须进入项目设置,然后选择适当的目标,然后单击“构建阶段”选项卡,然后展开“编译源”项,然后单击+符号,然后手动添加nsdata - extensions。m文件。

#4


0  

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.

我有这个错误信息,我正在使用可可豆。为了修复错误,我只需要再次调用pod install来正确创建所有必要的链接。

#5


0  

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.

您拥有category的实现,但没有接口。我是说你忘了在*里申报了。h你的类别的界面。

#6


0  

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.

我将代码从一个项目复制到另一个项目中,错误地将相同的源代码粘贴到.h文件和.m文件中(在这两个文件中,我都放置了用于.h文件的代码)。我修复了我的。m文件,它工作了。

#7


0  

May be because you write imageNamed instead of imageNamedDR in the interface..

可能是因为您在接口中编写了imageNamed而不是imag珐琅dr。

#1


202  

A couple possibilities:

两个可能性:

  1. You did not link UIImage+TTShare.m into your target. So while you have the header, you're not compiling the implementation.
  2. 你没有链接UIImage+TTShare。为你的目标。当你有了头,你没有编译实现。
  3. If this is part of a static library, you need to add -all_load to the Other Linker Flags build setting for the app linking against the library.
  4. 如果这是静态库的一部分,那么您需要将-all_load添加到另一个链接器标记为连接库的应用程序构建设置。

#2


6  

If you want to use Category method, you must add -ObjC to the Other Linker Flags build setting of your APP.

如果要使用Category方法,必须向应用程序的其他链接器标记构建设置添加-ObjC。

#3


5  

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.

我也遇到了同样的问题,必须应用这个修复。我的NSDate-Extensions。m源文件没有编译,所以我必须进入项目设置,然后选择适当的目标,然后单击“构建阶段”选项卡,然后展开“编译源”项,然后单击+符号,然后手动添加nsdata - extensions。m文件。

#4


0  

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.

我有这个错误信息,我正在使用可可豆。为了修复错误,我只需要再次调用pod install来正确创建所有必要的链接。

#5


0  

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.

您拥有category的实现,但没有接口。我是说你忘了在*里申报了。h你的类别的界面。

#6


0  

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.

我将代码从一个项目复制到另一个项目中,错误地将相同的源代码粘贴到.h文件和.m文件中(在这两个文件中,我都放置了用于.h文件的代码)。我修复了我的。m文件,它工作了。

#7


0  

May be because you write imageNamed instead of imageNamedDR in the interface..

可能是因为您在接口中编写了imageNamed而不是imag珐琅dr。