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

iOS*_*der 31 selector ios xcode5 semantics

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

#pragma GCC diagnostic ignored "-Wundeclared-selector"
Run Code Online (Sandbox Code Playgroud)

(来自:如何摆脱'未声明的选择器'警告)

编译器指令,但我的直觉说这不是这样做的合适方式.带有上述错误的小代码示例:

+ (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];
    }
}
Run Code Online (Sandbox Code Playgroud)

其中entityInManagedObjectContext:没有定义方法的地方.

有关如何最好地解决这些类型的错误的任何建议,提前谢谢?!

new*_*ima 25

是的你应该.

而不是这样做:

[self.searchResults sortUsingSelector:@selector(compareByDeliveryTime:)];
Run Code Online (Sandbox Code Playgroud)

你应该做这个:

SEL compareByDeliveryTimeSelector = sel_registerName("compareByDeliveryTime:");
[self.searchResults sortUsingSelector:compareByDeliveryTimeSelector];
Run Code Online (Sandbox Code Playgroud)

  • 任何想法*为什么*是默认的构建设置切换?调用`sel_registerName`(因此明确地`使用Objective-C运行时注册一个方法)会给你带来什么(除了额外的行)? (8认同)
  • 在我看来,这不会*修复*警告,它只是隐藏它.正确修复它应该包括声明选择器的文件,因为如果由于某种原因重命名该选择器,警告将重新出现,这是我们应该想要的. (2认同)

Abh*_*ert 20

您只需要声明包含选择器的类或协议.例如:

//  DeliveryTimeComparison.h
#import <Foundation/Foundation.h>

@protocol DeliveryTimeComparison <NSObject>

- (void)compareByDeliveryTime:(id)otherTime;

@end
Run Code Online (Sandbox Code Playgroud)

然后只需#import "DeliveryTimeComparison.h"在您计划使用的任何课程中@selector(compareByDeliveryTime:).

或者,只需为包含"compareByDeliveryTime:"方法的任何对象导入类头.


Jas*_*ane 15

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

  • 同意.但对于我们这些需要解决较大iOS 7问题的人来说,这可以让你隐藏它们,直到你以后可以处理它们为止.使用这些警告提交应用程序没有问题. (7认同)
  • 这没有回答关于如何修复警告的问题,而是阻止警告发生.. -1为此! (6认同)

小智 10

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

另一种选择当然是使用ignore块专门包围该代码

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
Run Code Online (Sandbox Code Playgroud)

最后

#pragma clang diagnostic pop
Run Code Online (Sandbox Code Playgroud)