user875293
user875293

Reputation: 679

"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];
}

Upvotes: 1

Views: 13306

Answers (2)

Jim Rhodes
Jim Rhodes

Reputation: 5095

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];
}

Upvotes: 2

Kjuly
Kjuly

Reputation: 35191

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;

Upvotes: 1

Related Questions