我试图从我的本地节点服务器提取一些数据.服务器获取get请求并记录它,但由于某种原因,我的iOS应用程序将不会执行我在完成处理程序中的任何代码.这是代码:
- (IBAction) buttonPressed{
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1:3000/"];
NSURLSessionDataTask *dataTask =
[self.session dataTaskWithURL:url
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error){
nameLabel.text = @"yay!";
/*
if (!error){
nameLabel.text = @"noerr";
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse *)response;
if (httpResp.statusCode == 200){
NSError *jsonErr;
NSDictionary *usersJSON =
[NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&jsonErr];
if (!jsonErr){
// nameLabel.text = usersJSON[@"username"];
nameLabel.text = @"nojerr";
}
else{
nameLabel.text = @"jsonErr";
}
}
}
else{
nameLabel.text = @"Err";
}
*/
}];
[dataTask resume];
Run Code Online (Sandbox Code Playgroud)
}
程序运行时,nameLabel不会更改为"yay".但是,如果我尝试在NSURLSessionDataTask行之前更改nameLabel,则会更改.