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

穿精又带淫゛_ 提交于 2019-11-27 12:20:45

问题


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?!


回答1:


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.




回答2:


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];



回答3:


Xcode 5 turned this on by default. To turn it off go to "Build Settings" for your target under "Apple LLVM 5.0 - Warnings - Objective C" -> "Undeclared Selector" set it to "NO". This should take care of it.




回答4:


These selector warnings in MagicalRecord are for compatibility with mogenerator's generated Core Data classes. Besides using mogenerator and perhaps importing one of the entities there really isn't much you can do besides what was already answered.

Another option of course is to surround that code specifically with ignore blocks

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"

and at the end

#pragma clang diagnostic pop


来源:https://stackoverflow.com/questions/18570907/should-i-fix-xcode-5-semantic-issue-undeclared-selector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!