如何返回完成块中填充的数组?将一些JSON解析为一个对象数组。
-(NSArray *)GetMapVehiclePoints
{
NSString *methodURL = [NSString stringWithFormat:@"%@GetMapVehiclePoints", BASE_URL];
__block NSArray *parsedVehicles = [[NSArray alloc]init];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:methodURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSArray *vehicles = (NSArray *)responseObject;
parsedVehicles = [self parseVehicles:vehicles];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
return parsedVehicles;
}
每次我返回这个方法时,我得到一个空数组,因为完成块还没有运行。如何在块内返回数组?
答案 0 :(得分:1)
getMapVehiclePoints
无法/不应该返回一个数组。请改用回调块......
typedef void(^CallbackBlock)(id value);
- (void)getMapVehiclePointsWithCallback:(CallbackBlock)callback {
NSString *methodURL = [NSString stringWithFormat:@"%@GetMapVehiclePoints", BASE_URL];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:methodURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (callback) callback([self parseVehicles:(id)responseObject]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
if (callback) callback(@[]);
}];
}
[self getMapVehiclePointsWithCallback:^(NSArray *vehiclePoints) {
NSLog(@"%@", vehiclePoints);
}];
答案 1 :(得分:1)
我传递的是方法的完成块
- (void)GetMapVehiclePointsWithCompletion:(void (^)(NSArray *points))completion;