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

时间:2012-04-25 06:56:42

标签: iphone objective-c ios uiviewcontroller incompatibility

我有一个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

在myPlugin我有

-(ViewController*) getPluginViewController {
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    return self.viewController;

在ViewController中我有

@interface ViewController : UIViewController {

3 个答案:

答案 0 :(得分:3)

您的应用委托中的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被自动释放两次。

答案 1 :(得分:1)

注意双重分配。

首次使用[myPlugin alloc]分配并致电getPluginViewController。 但是在getPluginViewController中,您分配并初始化新的ViewController并将其返回。

答案 2 :(得分:-1)

删除ViewController以及您认为有问题的其他类的引用。

如果需要,转到查找程序并通过取消选中'-copy'再次添加这些类。

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