将NSError**作为方法参数传递时的编译器警告

spl*_*cer 0 cocoa objective-c compiler-warnings compiler-bug

在过去的4个小时里,我一直在摸不着头脑,尝试各种小实验,但我似乎无法弄清楚出了什么问题.这可能是编译器错误吗?

Test.m:

- (id)initWithContentsOfURL:(NSURL *)aURL error:(NSError **)error
{
    if (!(self = [super init])) {
        return nil;
    }
    return self;
}
Run Code Online (Sandbox Code Playgroud)

main.m文件:

NSError *error;

Test *t = [[Test alloc] initWithContentsOfURL:[NSURL fileURLWithPath:@"/"] error:&error];
Run Code Online (Sandbox Code Playgroud)

这是编译器警告(来自main.m):

警告:不兼容的Objective-C类型'struct NSError**',当从'initWithContentsOfURL:error:'传递参数2时,期望'struct NSDictionary**'来自不同的Objective-C类型

我正在使用最新版本的Xcode和Snow Leopard.

phi*_*red 5

我怀疑它正在选择一个不同的选择器实例,initWithContentsOfURL:error:也许是其中的一个NSAppleScript.请记住,[NSObject alloc]返回一个id.

您的代码在运行时是否按预期工作?

尝试铸造返回[Test alloc]Test*.


Test *t = [(Test*)[Test alloc] initWithContentsOfURL:[NSURL fileURLWithPath:@"/"] error:&error];
Run Code Online (Sandbox Code Playgroud)

  • 你说的没错.Objective-C不喜欢具有相同签名的方法具有不同的类型(即它不进行重载),因此如果它们采用不同的参数,则避免将两个方法命名为相同. (3认同)