如果说我是 Objective-C 的新手,那就太轻描淡写了。我主要是一名 Ruby/Rails 开发人员,当谈到 OOP 和一般编程时,它完全宠坏了我。
在厌倦了阅读教程之后,我决定尝试使用 NSRULSession 来访问我的一个 Rails 应用程序(Elder Scrolls Online 技能规划器)并在我的 iOS 应用程序上显示一些 JSON 响应。代表没有意义,我不确定如何将此功能分解为方法等,所以我想我会保持简单并在 viewDidLoad() 方法中完成所有操作(是的,我知道这是不好的做法)。
- (void)viewDidLoad {
[super viewDidLoad];
__block NSDictionary *skillData; // No clue what __block is
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"http://www.esomix.com/skill_builds/17.json"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@", json[@"name"]);
skillData = json;
}];
[dataTask resume];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
myLabel.textColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1];
[self.view addSubview:myLabel];
NSLog(@"%@", skillData[@"name"]);
NSLog(@"bottom of method");
}
经过大量的玩弄之后,我发现尽管 NSURLSession 代码位于底部的 NSLogs 之前,但它会在渲染后返回其数据。难怪我的 label.text(未显示)没有设置好!这是我的三个 NSLog 的顺序:
(null)
bottom of method
Single-Target Lockdown Sniper
我想我的问题是在数据返回后发出 JSON API 请求并使用数据生成 UI 元素/其他任务的最简单、正确的方法是什么。非常感谢!