Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I fix Xcode 5 'Semantic issue: undeclared selector'?

I'm trying to upgrade my app with Xcode5 but encountered a number of 'Semantic issues' in a third party library (being MagicalRecord). The quickest way to 'fix' this might be using the:

#pragma GCC diagnostic ignored "-Wundeclared-selector" 

(from: How to get rid of the 'undeclared selector' warning)

compiler directive, but my gut-feeling says this is not the appropriate way to do this. A small code sample with the above error:

+ (NSEntityDescription *) MR_entityDescriptionInContext:(NSManagedObjectContext *)context {      if ([self respondsToSelector:@selector(entityInManagedObjectContext:)])      {         NSEntityDescription *entity = [self performSelector:@selector(entityInManagedObjectContext:) withObject:context];         return entity;     }     else     {         NSString *entityName = [self MR_entityName];         return [NSEntityDescription entityForName:entityName inManagedObjectContext:context];     } } 

where the entityInManagedObjectContext: method is not defined anywhere.

Any suggestions on how to best fix these types of errors, thanks in advance?!

like image 545
iOS-Coder Avatar asked Sep 02 '13 10:09

iOS-Coder


People also ask

What is the meaning of undeclared selector?

The intent of the "undeclared selector" warning is to catch errors at compile time if you change the name of the selector you were relying on. So it is most correct to #import the file that declares the method you were relying on.

Why is Xcode complaining that a specific identifier doesn't exist?

If Xcode is complaining that a specific identifier doesn’t exist, then check that all of your opening curly braces have corresponding closing curly braces. If you look at the screenshot below, both build errors are caused because of missing curly braces.

What does “unresolved” mean in Xcode?

You will often see this error if Xcode cannot find the variable or property you are referring to. “Unresolved” means that Xcode couldn’t find something, and “identifier” is another word for a variable, property, function name, etc. Here are several reasons why you could receive this error:

Why does my Xcode Interface look different?

My Xcode Interface Is Different 1. Xcode Simulator Errors My iPhone simulator looks different? If your iPhone simulator doesn’t look like the one you see me using in my videos, it’s because your iOS simulator is at a different zoom level.


2 Answers

Yes you should.

instead of doing this:

[self.searchResults sortUsingSelector:@selector(compareByDeliveryTime:)]; 

you should do this:

SEL compareByDeliveryTimeSelector = sel_registerName("compareByDeliveryTime:"); [self.searchResults sortUsingSelector:compareByDeliveryTimeSelector]; 
like image 171
newton_guima Avatar answered Sep 24 '22 06:09

newton_guima


You just need to declare a class or protocol that contains the selector. For example:

//  DeliveryTimeComparison.h #import <Foundation/Foundation.h>  @protocol DeliveryTimeComparison <NSObject>  - (void)compareByDeliveryTime:(id)otherTime;  @end 

And then simply #import "DeliveryTimeComparison.h" in any class where you plan to use @selector(compareByDeliveryTime:).

Or alternatively, just import the class header for any object that contains a "compareByDeliveryTime:" method.

like image 25
Abhi Beckert Avatar answered Sep 22 '22 06:09

Abhi Beckert