【发布时间】:2014-08-29 13:13:16
【问题描述】:
我很难理解如何调用方法。代码如下:
- (void)beachJSON
{
// Build the string to call Beach API
NSString *urlString = [NSString stringWithFormat:@"http://nrw-bwq-dev-api.azurewebsites.net/api/Pull"];
// Create NSURL string from formatted string
NSURL *url = [NSURL URLWithString:urlString];
// Setup and start async download
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection release];
[request release];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Store incoming data into a string
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
results = [jsonString JSONValue];
}
- (NSArray *)getBeachList {
[self beachJSON];
return results;
}
我从另一个类调用 getBeachList 来填充“结果”,beachJSON 调用很好,但我需要调用
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
内
- (NSArray *)getBeachList
即就在这里
- (NSArray *)getBeachList {
[self beachJSON];
//This is where I want to call it to populate results before it is returned
return results;
}
调用 getBeachList 将调用 beachJSON,但连接方法将被跳过而留下 'results' nil
如果我尝试简单地放置
[self connection:(NSURLConnection *)connection didReceiveData:(NSData *)data]
我在该行收到预期的表达式错误。
我对目标 c 很陌生,所以任何帮助都会很棒。
【问题讨论】:
标签: objective-c json ios7 methods xcode5