gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:07:24 25 4
gpt4 key购买 nike

我一直在遵循有关使用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];
}

关于ios - Objective-C中的“使用未声明的标识符”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18832322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com