[英]Incompatible pointer type assigning to 'ViewController'
我有一个ViewController类(也许我不应该这样命名那个类?)
为什么我会收到警告
从AppDelegate中的“UIViewController”分配给“ViewController”的指针类型不兼容
更新:
在这条线上
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 {
应用程序委托中的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”开头的方法用于返回指针参数中的值的方法。 但是,将它与您调用它的地方放在一起有几个问题:
注意双重分配。
第一次使用[myPlugin alloc]
分配并调用getPluginViewController
。 但是在getPluginViewController
您可以分配并初始化新的ViewController
并将其返回。
删除ViewController
和您认为有问题的其他类的引用。
如果需要,转到查找程序并通过取消选中'-copy'再次添加这些类。
从产品菜单中清除项目并运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.