1

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

2 回答 2

2

您需要navController在 if 语句之外声明:

- (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];
}
于 2013-09-16T16:02:31.213 回答
1

您需要声明navControllerif-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;
于 2013-09-16T16:01:18.333 回答