2010-03-09 21 views
2

这里是我得到编译器警告的函数,我似乎无法弄清楚是什么导致它。任何帮助表示赞赏。帮助编译器警告:当类型匹配时从不同的Objective-C类型初始化

-(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; 
} 
+0

我记得得到这个错误,问题是我没有导入我需要的头文件。 – 2010-03-09 02:15:38

回答

1

我猜这是不是在你的项目中有一个initWithCreator:方法的唯一类。一般来说,将静态类型赋给init方法是一个坏主意。 alloc返回id,所以编译器不知道你要发送init方法的对象的类型。如果有多个选择,它通常会猜错,并且会看到您看到的警告。

+0

谢谢,这正是问题所在。我的解决方案基于你的答案是当我调用initWithCreator时施放类型: \t InstanceCreatorViewController * controller = [(InstanceCreatorViewController *)[InstanceCreatorViewController alloc] initWithCreator:self]; – 2010-03-09 02:42:57

+0

,演员阵中有一个星号,不知道它去了哪里。 – 2010-03-09 03:04:38

+0

@Alex Gosselin:Stack Overflow使用Markdown,它在强调的星号之间解析文本。要使代码格式正确,请将它放在反引号之间。所以这将是(反向)'InstanceCreatorViewController * controller = [(InstanceCreatorViewController *)[InstanceCreatorViewController alloc] initWithCreator:self];'(反向)。 – Chuck 2010-03-09 03:51:27

相关问题