语义问题:指针不兼容警告

时间:2012-04-07 08:04:06

标签: iphone ios automatic-ref-counting compiler-warnings

我启用了ARC,在我的didFinishLaunchingWithOptions方法中,我编写了以下代码:

AppDelegate.h:

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;

@end

AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    ViewController * vc = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:vc];
    self.viewController = nav;
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

但声明:self.viewController = nav;收到编译警告,警告信息为:

file://.../AppDelegate.m: warning: Semantic Issue: Incompatible pointer types passing 'UINavigationController *__strong' to parameter of type 'ViewController *'

Compile Warning Information

如何删除警告?

感谢。

4 个答案:

答案 0 :(得分:2)

我假设ViewController是UIViewController的自定义子类,它与是UINavigationController本身的子类完全不同。这就是为什么它是错误的:超类不能完全充当它的子类(例如,它可能没有某些属性/方法等),因此警告。

答案 1 :(得分:1)

编译器告诉你:“nav,UINavigationController的一个实例,不是'ViewController'或'ViewController'的子类”。如果您真的想要同时使用导航控制器和视图控制器,则可以添加第二个属性:

@property (nonatomic, strong) UINavigationController *navController;

然后在application:didFinishLaunchingWithOptions:

中设置它
self.viewController = vc;
self.navController = nav;

这里的另一个解决方案就是抓住导航控制器并使用'topViewController'属性来访问你的VC。

编辑:或者更好的是,不关心导航控制器。只需:

self.viewController = vc;
self.window.rootViewController = nav;

答案 2 :(得分:1)

请尝试以下代码: -

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:self.viewController];

    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    return YES;
}

答案 3 :(得分:0)

你可以这样做

 self.viewController =[nav.viewControllers objectAtIndex:0];

然后它不会显示类似“不兼容的指针类型将'UINavigationController * __ strong'传递给'ViewController *'类型参数的警告