简体   繁体   中英

“use of undeclared identifier” error in Objective-C

I have been following a tutorial about parsing json with Xcode but have come across issues that i cant resolve. I suspect they are because the tutorial is using 5.1 and i am using 6.1

AppDelegate.m unused variable 'navController' use of undeclared identifier 'navController' See below

@implementation AppDelegate

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

ViewController.m Expected method body Use of undeclared identifier 'theData'

- (void)connection:NSURLConnection *)connection didReceiveData:(NSData *)theData
{
    [data appendData:theData];
}

You need to declare navController outside the if statement:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UINavigationController* navController;

    navController = nil;
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
        navController = [[UINavigationController alloc] initWithRootViewController: self.viewController];
    } else {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
        navController = [[UINavigationController alloc] initWithRootViewController: self.viewController];
    }
    self.window.rootViewController = navController;
    [self.window makeKeyAndVisible];
    return YES;
}

As an alternative, you could move the creation of navController so it only needs to be done in one place:

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

For the other error, you are missing '(' before NSURLConnection.

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
    [data appendData:theData];
}

You need to declare navController out of if-else :

UINavigationController *navController;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
    navController = [[UINavigationController alloc] initWithRootViewController: self.viewController];
} else {
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
    navController = [[UINavigationController alloc] initWithRootViewController: self.viewController];
}
self.window.rootViewController = navController;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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