ios 我应该修复 Xcode 5 '语义问题:未声明的选择器'吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18570907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:56:16  来源:igfitidea点击:

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

iosselectorsemanticsxcode5

提问by iOS-Coder

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:

我正在尝试使用 Xcode5 升级我的应用程序,但在第三方库(即 MagicalRecord)中遇到了许多“语义问题”。“解决”这个问题的最快方法可能是使用:

#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.

其中entityInManagedObjectContext:方法未在任何地方定义。

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

关于如何最好地修复这些类型的错误的任何建议,提前致谢?!

采纳答案by Abhi Beckert

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:).

然后只需#import "DeliveryTimeComparison.h"在您计划使用@selector(compareByDeliveryTime:).

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

或者,只需导入包含“compareByDeliveryTime:”方法的任何对象的类头。

回答by newton_guima

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

回答by Jason Shehane

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.

Xcode 5 默认开启了这个功能。要关闭它,请转到“Apple LLVM 5.0 - 警告 - 目标 C”->“未声明的选择器”下目标的“构建设置”将其设置为“否”。这应该照顾它。

回答by Keyd

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.

MagicalRecord 中的这些选择器警告是为了与 mogenerator 生成的 Core Data 类兼容。除了使用 mogenerator 并可能导入其中一个实体之外,除了已经回答的内容之外,您真的无能为力。

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