【发布时间】:2015-06-18 01:00:59
【问题描述】:
我尝试使用afnetworking进行简单的测试,但是似乎根本没有触发成功/失败blcok,是不是我忘了设置什么?
我的代码如下:
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *string = [NSString stringWithFormat:@"%@weather.php?format=json", BaseURLString];
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Succeed!");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Fail");
}];
[operation start];
}
return 0;
}
更新:我尝试使用循环 a 来等待操作执行并打印出来,但没有打印出来。这是我的更新版本
int main(int argc, const char * argv[]) {
@autoreleasepool {
__block bool result = false;
NSString *string = [NSString stringWithFormat:@"%@weather.php?format=json", BaseURLString];
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Succeed!");
result = true;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Fail");
result = true;
}];
[operation start];
while (1) {
if (result) {
break;
}
}
}
return 0;
}
【问题讨论】:
标签: ios objective-c iphone macos afnetworking