Objective-C Category Cause Unrecognized Selector

My project has a function of the UIImage category that I want to call from another class. I am importing the header file for the image category correctly, and I want the project to compile without warning.

The problem is that when I call the function of the UIImage category, I saw an unrecognized selector error using NSInvalidArgumentException . Why do I see this if I connected everything correctly?

 #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 

which calls it:

  backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamedDR:@"Share Popup Background"]]; 

an exception:

 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 
+63
objective-c iphone cocoa category
Oct 22 2018-10-22
source share
7 answers

A couple of features:

  • You have not specified UIImage+TTShare.m in your target. Therefore, when you have a header, you are not compiling the implementation.
  • If this is part of a static library, you need to add -all_load to the Other flag -all_load option for the library-related application.
+195
Oct 22 '10 at 16:05
source share

If you want to use the Category method, you must add -ObjC to the build configuration of the other flag composers of your APP.

+5
Apr 28 '15 at 13:58
source share

I had the same problem and had to apply this fix. My source file NSDate-Extensions.m did not compile, so I had to go to “Project Settings”, then select the appropriate target, then click on the “Phase Assembly” tab, then expand the “Compile Sources” elements, and then click the + symbol and manually add the NSDate-Extensions.m file.

+4
Mar 11 '13 at 4:42 on
source share

I got this error message and am using Cocoapods. To fix the error, I just had to call pod install again to correctly create all the necessary links.

0
Apr 09 '15 at 10:48
source share

Another opportunity.

You have a category implementation, but you do not have an interface. I mean, you forgot to declare the interface of your category in * .h.

0
Aug 27 '15 at 9:00
source share

Another possibility:

It's almost too embarrassing to admit, but just in case, someone could make the same stupid mistake:

I copied the code from one project to another, and by mistake I pasted the same source code in the .h file and in the .m file (in both I put the code intended for the .h file). I fixed the .m file and it worked.

0
Sep 02 '16 at 15:40
source share

Maybe because you write imageNamed instead of imageNamedDR in the interface.

0
Nov 11 '16 at 2:59
source share



All Articles