How to resolve 'unrecognized selector sent to instance'?

189,721

Solution 1

1) Is the synthesize within @implementation block?

2) Should you refer to self.classA = [[ClassA alloc] init]; and self.classA.downloadUrl = @"..." instead of plain classA?

3) In your myApp.m file you need to import ClassA.h, when it's missing it will default to a number, or pointer? (in C variables default to int if not found by compiler):

#import "ClassA.h".

Solution 2

Set flag -ObjC in Other linker Flag in your Project setting... (Not in the static library project but the project you that is using static library...) And make sure that in Project setting Configuration is set to All Configuration

Solution 3

A lot of people have given some very technical answers for this and similar questions, but I think it's simpler than that. Sometimes if you're not paying attention a selector that you don't intend to use can be attached to something in the interface. You might be getting this error because the selector's there but you haven't written any code for it.

The easiest way to double-check that this is not the case is to control-click the item so you can see all of the selectors that are associated with it. If there's anything in there that you don't want to be, get rid of it! Hope this helps...

Solution 4

Mine was something simple/stupid. Newbie mistake, for anyone that has converted their NSManagedObject to a normal NSObject.

I had:

@dynamic order_id;

when i should have had:

@synthesize order_id;

Solution 5

For me, what caused this error was that I accidentally had the same message being sent twice to the same class member. When I right clicked on the button in the gui, I could see the method name twice, and I just deleted one. Newbie mistake in my case for sure, but wanted to get it out there for other newbies to consider.

Share:
189,721
4thSpace
Author by

4thSpace

Updated on March 22, 2020

Comments

  • 4thSpace
    4thSpace over 4 years

    In the AppDelegate, I'm alloc'ing an instance defined in a static library. This instance has an NSString property set a "copy". When I access the string property on this instance, the app crashes with 'unrecognized selector sent to instance'. Xcode provides a code hint for the property, which means it is known in the calling app. The particular class is compiled into the static library target. What am I missing?

    Adding some code.

    //static library 
    //ClassA.h
    @interface ClassA : NSObject {
    ...
    NSString *downloadUrl;
    }
    @property(nonatomic, copy) NSString *downloadUrl;
    
    //ClassA.m
    @synthesize downloadUrl;
    

    In the calling app's appDelegate.

    //app delegate header file
    @interface myApp : NSObject <UIApplicationDelegate> {
    ClassA *classA;
    }
    @property (nonatomic, retain) ClassA *classA;
    
    //app delegate .m file
    @synthesize classA;
    
    - (void)applicationDidFinishLaunching:(UIApplication *)application {
    classA = [[ClassA alloc] init];
    //exception occurs here.  downloadUrl is of type NSCFNumber
    classA.downloadUrl = @"http://www.abc.com/";
    ...}
    

    Other classes in the app will get a reference to the delegate and call classA.downloadUrl.

  • 4thSpace
    4thSpace about 15 years
    In the static class I have @property(nonatomic, copy) NSString *theString and theString is @synthesize. Is there something else?
  • 4thSpace
    4thSpace about 15 years
    I do notice when I mouse over theString on the caller side, its type is NSCFNumber. Should be NSString.
  • 4thSpace
    4thSpace about 15 years
    In the library, I have a .h that references all the other .h files. So, the calling app only needs to reference that particular .h file to get a reference to everything in the library. I can drag the ClassA header file into the app project and reference it from the .m file. Still get the same exception though.
  • Heat Miser
    Heat Miser about 15 years
    Another thing you may want to try is doing self.classA.downloadUrl. That error is usually what you get when a variable is initialized to nothing, such as NSString *m. It gets set to some seemingly random int. You might try overriding the init method in the class and setting it to nil in there.
  • 4thSpace
    4thSpace about 15 years
    I've tried overriding init but my breakpoint in there is never hit. However, if I Jump to Definition on ClassA where the alloc is, it goes to the correct file and I see my custom init.
  • 4thSpace
    4thSpace about 15 years
    Have already tried that. Still get exception. I have several other similar classes in the same library. None of them are having this issue.
  • Nubzor
    Nubzor about 15 years
    ... Of course the 'www' was shortened to web link in previous comment
  • 4thSpace
    4thSpace about 15 years
    I have some NSInteger properties in ClassA that are all declared the same way. I can set only one of them. The others throw the same type of exception.
  • Nubzor
    Nubzor about 15 years
    Is the synthesize within @implementation block? should you refer to self.classA = [[ClassA alloc] init]; and self.classA.downloadUrl = @"..." ?
  • 4thSpace
    4thSpace about 15 years
    I've added a test string property to another class in the static library. I get the same results. Somewhere, the linking up must have changed.
  • 4thSpace
    4thSpace about 15 years
    Yes - but still same exception. Yes to #1 above. For #3, I put imports in the PCH file so each classes doesn't have to keep importing.
  • 4thSpace
    4thSpace about 15 years
    If I open the ClassA.o file in the calling app's build folder, I see getter and setter methods for downloadUrl.
  • 4thSpace
    4thSpace about 15 years
    Finally I have resolved it. I removed ClassA.h from the .h file in the library which references all .h files. I then dragged ClassA.h into the app project and added the imports. I was trying to follow the technique mentioned here github.com/joehewitt/three20/tree/master with his common.h. The weird thing is I still have lots of classes referenced through my common.h, which still work in the app project. I didn't have to drag their header files over
  • Heat Miser
    Heat Miser about 15 years
    I see below that you solved it. I think there is something strange about the linker with Objective-C classes vs normal C++ classes. Typically even though they are compiled in, I like to keep the header files in the main project, whenever I don't I seem to have problems like this. I'm glad you solved it!
  • Takagi
    Takagi over 12 years
    I just created a static library with a unittest target in Xcode4.2. I had to add "-ObjC" to "Other Linker Flags". Now the unittest runs.
  • jeremytripp
    jeremytripp about 10 years
    At the end of a long day, this was exactly what I needed to see. Thanks for keeping it simple.
  • Martin Bean
    Martin Bean almost 10 years
    Just had this in my first iOS project. I had a dead “link” to an action implementation in my view controller. Removed it, and it worked. Thanks!
  • Jubei
    Jubei almost 9 years
    @stefanB Thanks for the answer! In my case the problem was that I was not referencing self. , but I'm curious, if that's so important, why doesn't swift throw a compiler error?
  • Kenny
    Kenny over 8 years
    Bingo - exactly what I needed! Thank you!
  • Diego Carrera
    Diego Carrera about 5 years
    Exactly, in my case was a different capitalization in .h and .m, in .h it was methodName() and in .m was MethodName()
  • alstr
    alstr about 3 years
    Figured this out before seeing this answer, but it's worth checking. I had 2 outlets set for a UIImageView.