ios - Returning from a block - Objective C -
i have class method containing block, of afnetworking in want return 1 dictionary variable, code shown below:
+(nsmutabledictionary*) getfromurl:(nsstring*)url parameterspassed:(nsdictionary*)parameters; { afhttprequestoperationmanager *manager = [afhttprequestoperationmanager manager]; __block nsmutabledictionary *resultarray; [manager get:url parameters:parameters success:^(afhttprequestoperation *operation, id responseobject) { nslog(@"json: %@", responseobject); nsmutabledictionary *resultarraytwo = (nsmutabledictionary *)responseobject; resultarray = resultarraytwo; } failure:^(afhttprequestoperation *operation, nserror *error) { nslog(@"error: %@, %@", error, operation.responsestring); uialertview *alertview=[[uialertview alloc] initwithtitle:@"message" message:@"try again" delegate:nil cancelbuttontitle:@"ok" otherbuttontitles: nil]; [alertview show]; }]; return resultarray; } how can return resultarray here, returns nothing here due difference in control flow.
i don't have knowledge in objective c block. waiting help.
thank you.
change function design following function using completion blocks
+(void)getfromurl:(nsstring*)url parameterspassed:(nsdictionary*)parameters completion:(void (^) (nsmutablearray *values))completion; { afhttprequestoperationmanager *manager = [afhttprequestoperationmanager manager]; [manager get:url parameters:parameters success:^(afhttprequestoperation *operation, id responseobject) { nslog(@"json: %@", responseobject); nsmutabledictionary *resultarray = (nsmutabledictionary *)responseobject; completion(resultarray); } failure:^(afhttprequestoperation *operation, nserror *error) { nslog(@"error: %@, %@", error, operation.responsestring); uialertview *alertview=[[uialertview alloc] initwithtitle:@"message" message:@"try again" delegate:nil cancelbuttontitle:@"ok" otherbuttontitles: nil]; [alertview show]; completion(nil); }]; } and call function
[yourclass getfromurl:url parameterspassed:params completion:^(nsmutablearray *values) { nslog(@"%@",values); }]; update removed array used.
hope helps.
Comments
Post a Comment