简体   繁体   English

Objective-C中的“使用未声明的标识符”错误

[英]“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. 我一直在遵循有关使用Xcode解析json的教程,但是遇到了我无法解决的问题。 I suspect they are because the tutorial is using 5.1 and i am using 6.1 我怀疑是因为教程使用的是5.1而我使用的是6.1

AppDelegate.m unused variable 'navController' use of undeclared identifier 'navController' See below 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 Expected method body Use of undeclared identifier 'theData' ViewController.m预期的​​方法主体使用未声明的标识符'theData'

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

You need to declare navController outside the if statement: 您需要在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;
}

As an alternative, you could move the creation of navController so it only needs to be done in one place: 或者,您可以移动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;
}

For the other error, you are missing '(' before NSURLConnection. 对于另一个错误,您在NSURLConnection之前缺少'('。

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

You need to declare navController out of if-else : 您需要在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.

相关问题 Objective-C执行未声明的标识符错误 - Objective-C exercises undeclared identifier error 将Swift导入Objective-C-“使用未声明的标识符” - Import Swift into Objective-C - “use of undeclared identifier” iOS:Objective-C创建类属性错误:使用未声明的标识符 - iOS: Objective-C creating class property error: Use of undeclared identifier 在目标C中使用未声明的标识符 - Use of undeclared identifier in Objective C Objective-C:未声明的标识符applicationDidEnterBackground - Objective-c: undeclared identifier applicationDidEnterBackground 伪造具有关联引用的Objective-C类别中的实例变量 - 错误“使用未声明的标识符'OBJC_ASSOCIATION_RETAIN” - Faking instance variables in Objective-C categories with Associative References - Error “Use of undeclared identifier 'OBJC_ASSOCIATION_RETAIN” 在将快速代码移植到Objective-C项目后使用未声明的标识符 - Use of undeclared identifier after porting swift code to objective-C project 未声明的标识符目标C. - Undeclared identifier Objective C Objective-C++ AppDelegate.mm 中未声明的标识符 Objective-C - Undeclared identifier Objective-C in Objective-C++ AppDelegate.mm iOS-目标-C Google Maps使用未声明的标识符 - iOS - Objective - C Google Maps use of undeclared identifier
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM