簡體   English   中英

Objective-C中的“使用未聲明的標識符”錯誤

[英]“use of undeclared identifier” error in Objective-C

我一直在遵循有關使用Xcode解析json的教程,但是遇到了我無法解決的問題。 我懷疑是因為教程使用的是5.1而我使用的是6.1

AppDelegate.m未使用的變量'navController'使用未聲明的標識符'navController'參見下文

@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預期的​​方法主體使用未聲明的標識符'theData'

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

您需要在if語句之外聲明navController

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

或者,您可以移動navController的創建,因此只需要在一個地方完成:

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

對於另一個錯誤,您在NSURLConnection之前缺少'('。

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

您需要在if-else聲明navController

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM