0

我将以下代码作为 iOS 应用程序的一部分,该应用程序从 URL 获取 JSON 数组并使用它来填充表格视图。出于某种原因,函数中没有发出请求,我不知道为什么。功能如下图。

PS - 我是这个 Objective-C 云雀的新手,如果我犯了一个非常明显的错误,请原谅我!

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    // Create a URL Request
    self.responseData = [NSMutableData data];
        NSURLRequest *request = [NSURLRequest requestWithURL:
    [NSURL URLWithString:@"http://mydomain.com/return_json_list"]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];

    self.viewController = [[BIDViewController alloc] initWithNibName:@"BIDViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}
4

1 回答 1

0

你从来没有真正提出请求......你需要在或(同步) start上调用(异步)。NSURLConnection+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error

您可以通过以下方式完成后者:

self.responseData = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://mydomain.com/return_json_list"]];

请注意,它将发生在主线程上,因此您的 UI 会受到影响。

于 2013-02-26T21:56:19.700 回答