简体   繁体   English

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

[英]Incompatible pointer type assigning to 'ViewController'

I have a ViewController class (maybe I shouldn't have named that class that way ?) 我有一个ViewController类(也许我不应该这样命名那个类?)

Why do I have a warning 为什么我会收到警告

Incompatible pointer type assigning to 'ViewController' from 'UIViewController' in AppDelegate 从AppDelegate中的“UIViewController”分配给“ViewController”的指针类型不兼容

Update: 更新:

at this line 在这条线上

self.viewController = [[[myPlugin alloc] getPluginViewController] autorelease];

in AppDelegate.h I have 在AppDelegate.h我有

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;

@end

in myPlugin I have 在myPlugin我有

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

In ViewController I have 在ViewController中,我有

@interface ViewController : UIViewController {

The viewController property in your app delegate probably has type UIViewController* and you are trying to assign an object of type ViewController* to it. 应用程序委托中的viewController属性可能具有类型UIViewController*并且您尝试为其分配类型为ViewController*的对象。 Probably your ViewController class needs to inherit from UIViewController. 可能你的ViewController类需要从UIViewController继承。

There are many other problems with your code: 您的代码还有许多其他问题:

self.viewController = [[[myPlugin alloc] getPluginViewController] autorelease];

Ignoring the assignment, the first message sent to an object straight after it has been allocated should be an init message by convention. 忽略赋值,在分配后直接发送给对象的第一条消息应该是按惯例的init消息。 99.99% of programmers will automatically assume this is a horrendous bug in your code whether or not it is a horrendous bug. 99.99%的程序员会自动认为这是代码中的一个可怕的错误,无论它是否是一个可怕的错误。 You should stick to the conventions. 你应该坚持惯例。

Also, if getPluginViewController abides by the memory management rules, you do not own the object it returns, so you must not autorelease it. 此外,如果getPluginViewController遵守内存管理规则,则您不拥有它返回的对象,因此您不能自动释放它。

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

In itself, this is kind of OK. 就其本身而言,这是好的。 In Objective-C, by convention, a method beginning with "get" is for methods that return values in pointer parameters. 在Objective-C中,按照惯例,以“get”开头的方法用于返回指针参数中的值的方法。 However, putting it together with where you call it there are several problems: 但是,将它与您调用它的地方放在一起有几个问题:

  • the original allocated ViewController leaks because this method returns a pointer to a different object 原始分配的ViewController泄漏,因为此方法返回指向其他对象的指针
  • the original allocated ViewController is never initialised 从未初始化原始分配的ViewController
  • the returned ViewController is autoreleased twice. 返回的ViewController被自动释放两次。

Pay attention at double allocating. 注意双重分配。

First time you allocating with [myPlugin alloc] and calling getPluginViewController . 第一次使用[myPlugin alloc]分配并调用getPluginViewController But in getPluginViewController you allocate and initialize new ViewController and return it. 但是在getPluginViewController您可以分配并初始化新的ViewController并将其返回。

Remove the reference of ViewController and other classes which you think have problem. 删除ViewController和您认为有问题的其他类的引用。

Go to the finder and add again those classes by unchecking '-copy' if needed. 如果需要,转到查找程序并通过取消选中'-copy'再次添加这些类。

Clean the project from product menu and run. 从产品菜单中清除项目并运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM