在过去的 4 个小时里,我一直在为此绞尽脑汁,尝试各种小实验,但我似乎无法弄清楚出了什么问题。这可能是编译器错误吗?
测试.m:
- (id)initWithContentsOfURL:(NSURL *)aURL error:(NSError **)error
{
if (!(self = [super init])) {
return nil;
}
return self;
}
main.m:
NSError *error;
Test *t = [[Test alloc] initWithContentsOfURL:[NSURL fileURLWithPath:@"/"] error:&error];
这是编译器警告(来自 main.m):
warning: incompatible Objective-C types 'struct NSError **', expected 'struct NSDictionary **' when passing argument 2 of 'initWithContentsOfURL:error:' from distinct Objective-C type
我正在使用最新版本的 Xcode 和 Snow Leopard。
最佳答案
我怀疑它选择了不同的选择器实例,initWithContentsOfURL:error:
- 可能是 NSAppleScript
中的一个。 。请记住,[NSObject alloc]
返回一个 id
。
您的代码在运行时是否按预期工作?
尝试将 [Test alloc]
的返回值转换为 Test*
。
即
Test *t = [(Test*)[Test alloc] initWithContentsOfURL:[NSURL fileURLWithPath:@"/"] error:&error];
关于objective-c - 将 NSError ** 作为方法参数传递时出现编译器警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2591820/