[英]“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.