编译器警告帮助:当类型匹配时,从不同的Objective-C类型初始化
这是我得到编译器警告的函数,我似乎不知道是什么导致了它。感谢您的帮助编译器警告帮助:当类型匹配时,从不同的Objective-C类型初始化,objective-c,iphone,oop,compiler-warnings,Objective C,Iphone,Oop,Compiler Warnings,这是我得到编译器警告的函数,我似乎不知道是什么导致了它。感谢您的帮助 -(void)displaySelector{ //warning on the following line: InstanceSelectorViewController *controller = [[InstanceSelectorViewController alloc] initWithCreator:self]; [self.navController pushViewControlle
-(void)displaySelector{
//warning on the following line:
InstanceSelectorViewController *controller = [[InstanceSelectorViewController alloc] initWithCreator:self];
[self.navController pushViewController:controller animated:YES];
[controller release];
}
initWithCreator:方法的接口和实现
-(InstanceSelectorViewController*)initWithCreator:(InstanceCreator*)creator;
-(InstanceSelectorViewController*)initWithCreator:(InstanceCreator*)crt{
if (self = [self initWithNibName:@"InstanceSelectorViewController" bundle:nil]) {
creator = crt;
}
return self;
}
我猜这不是项目中唯一一个具有
initWithCreator:
方法的类。一般来说,为init方法提供静态类型是个坏主意alloc
返回id
,因此编译器不知道要向其发送init方法的对象的类型。如果有多个选项,它通常会猜错,您将看到警告。我记得出现此错误,问题是我没有导入所需的头文件。谢谢,这正是问题所在。基于您的回答,我的修复方法是在调用initWithCreator:InstanceCreatorViewController控制器=[(InstanceCreatorViewController)[InstanceCreatorViewController alloc]initWithCreator:self]时强制转换类型;该演员阵容中有一个星号,不知道它去了哪里。@Alex Gosselin:Stack Overflow使用Markdown,它解析星号之间的文本,如强调的那样。要正确格式化代码,请将其放在反勾号之间。因此它将是(backtick)InstanceCreatorViewController*控制器=[(InstanceCreatorViewController*)[InstanceCreatorViewController alloc]initWithCreator:self]代码>(反勾)。哈哈,我的朋友,你有所有的答案。