我有一个带有 (id) 参数的 init 方法:
-(id) initWithObject:(id) obj;
我试图这样称呼它:
[[MyClass alloc] initWithObject:self];
但是 XCode 抱怨该参数是“不同的 Objective-C 类型”(这通常表示类型不匹配或间接错误级别)。
如果我明确地将 self 转换为 (id),警告就会消失。在任何一种情况下,代码都会按预期运行。有趣的是,在下一行中,我将 self 传递给另一个也采用 id 的方法,并且效果很好。
我想知道我是否遗漏了一些微妙的东西——或者它是编译器的一个特性?
在我确定它是必要的原因之前,我并不完全舒服地施放它。
[编辑]
我被要求提供更多代码。不确定还有很多其他相关的。这是我进行调用的实际代码。请注意,它本身位于 init 方法中。这是initWithSource
发出警告的电话:
-(id) initWithFrame:(CGRect) frame
{
self = [super initWithFrame: frame];
if( self )
{
delegate = nil;
touchDelegate = [[TBCTouchDelegate alloc] initWithSource:self];
[touchDelegate.viewWasTappedEvent addTarget: self action:@selector(viewWasTapped:)];
}
return self;
}
这是被调用的 init 方法:
-(id) initWithSource:(id) sourceObject
{
self = [super init];
if (self != nil)
{
// Uninteresting initialisation snipped
}
return self;
}