Incompatible pointer type assigning to 'ViewController'
-
03-06-2021 - |
Question
I have a ViewController class (maybe I shouldn't have named that class that way ?)
Why do I have a warning
Incompatible pointer type assigning to 'ViewController' from 'UIViewController' in AppDelegate
Update:
at this line
self.viewController = [[[myPlugin alloc] getPluginViewController] autorelease];
in AppDelegate.h I have
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@end
in myPlugin I have
-(ViewController*) getPluginViewController {
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
return self.viewController;
In ViewController I have
@interface ViewController : UIViewController {
Solution
The viewController property in your app delegate probably has type UIViewController*
and you are trying to assign an object of type ViewController*
to it. Probably your ViewController class needs to inherit from 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. 99.99% of programmers will automatically assume this is a horrendous bug in your code whether or not it is a horrendous bug. 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.
-(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. 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
- the original allocated ViewController is never initialised
- the returned ViewController is autoreleased twice.
OTHER TIPS
Pay attention at double allocating.
First time you allocating with [myPlugin alloc]
and calling getPluginViewController
.
But in getPluginViewController
you allocate and initialize new ViewController
and return it.
Remove the reference of ViewController
and other classes which you think have problem.
Go to the finder and add again those classes by unchecking '-copy' if needed.
Clean the project from product menu and run.