Iphone 分配给'的指针类型不兼容;ViewController';

Iphone 分配给'的指针类型不兼容;ViewController';,iphone,objective-c,ios,uiviewcontroller,incompatibility,Iphone,Objective C,Ios,Uiviewcontroller,Incompatibility,我有一个ViewController类(也许我不应该这样命名该类?) 我为什么要发出警告 从指定给“ViewController”的指针类型不兼容 AppDelegate中的“UIViewController” 更新: 在这条线上 self.viewController = [[[myPlugin alloc] getPluginViewController] autorelease]; 在AppDelegate.h中,我有 @class ViewController; @interface

我有一个ViewController类(也许我不应该这样命名该类?)

我为什么要发出警告

从指定给“ViewController”的指针类型不兼容 AppDelegate中的“UIViewController”

更新:

在这条线上

self.viewController = [[[myPlugin alloc] getPluginViewController] autorelease];
在AppDelegate.h中,我有

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;

@end
在ViewController中,我有

@interface ViewController : UIViewController {

注意双重分配

第一次使用
[myPlugin alloc]
分配并调用
getPluginViewController
时。
但是在
getPluginViewController
中,您分配并初始化新的
ViewController
并返回它。

应用程序委托中的ViewController属性可能具有类型
UIViewController*
,并且您正试图为其分配类型为
ViewController*
的对象。可能您的ViewController类需要从UIViewController继承

您的代码还有许多其他问题:

self.viewController = [[[myPlugin alloc] getPluginViewController] autorelease];
忽略分配,直接在分配对象后发送给对象的第一条消息按照约定应该是init消息。99.99%的程序员会自动认为这是代码中的一个可怕的错误,不管它是否是一个可怕的错误。你应该遵守惯例

此外,如果
getPluginViewController
遵守内存管理规则,则您不拥有它返回的对象,因此不能自动释放它

-(ViewController*) getPluginViewController {
     self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
     return self.viewController;
}
就其本身而言,这还行。在Objective-C中,按照惯例,以“get”开头的方法用于返回指针参数值的方法。但是,如果将其与您所称的位置放在一起,则存在几个问题:

  • 原始分配的ViewController泄漏,因为此方法返回指向不同对象的指针
  • 原始分配的ViewController从未初始化
  • 返回的ViewController将自动释放两次

删除
ViewController
和其他您认为有问题的类的引用

转到查找器,如果需要,取消选中“-copy”再次添加这些类


从产品菜单中清除项目并运行。

不,您不应该命名该类。如果您已将viewController定义为属性,您可以添加该代码吗?否则,如果您希望删除此警告,则需要解决方法-self.viewController=(viewController*)[[myPlugin alloc]getPluginViewController]autorelease];你能添加ViewController类的定义吗?我应该为我精确定义上下文:myPlugin类是一回事,myPluginViewController确实返回插件的suview,它没有itsef的主视图。在这种情况下,你认为还有什么问题吗?谢谢你的帮助。非常感谢。我现在看到了我的错误-我从来没有用C编程,所以直接跳到Objective C对我来说很难:)@user310291:是的,这可能是一个有点陡峭的学习曲线,但Objective-C是一种很好的语言,一旦你掌握了窍门。
-(ViewController*) getPluginViewController {
     self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
     return self.viewController;
}