objective c - How to access property in sub method after it runs? -


i'm having hard time working 1 out think should pretty simple. basically, have method talks webservice , need return data sub method, "authcode". doing wrong? how can authcode out of manager, or can create block or to ensure manager block runs first? using right words - blocks, sub methods??? please :)

- (nsstring *)getauthcodeexample { __block nsstring *returnstring = @"nothing yet!"; nsurl *baseurl = [nsurl urlwithstring:baseurlstring]; nsdictionary *parametersgetauthcode = @{@"req": @"getauth"};  afhttpsessionmanager *manager = [[afhttpsessionmanager alloc] initwithbaseurl:baseurl]; manager.responseserializer = [afjsonresponseserializer serializer];  [manager post:apiscript parameters:parametersgetauthcode success:^(nsurlsessiondatatask *task, id responseobject) {     if ([task.response iskindofclass:[nshttpurlresponse class]]) {         nshttpurlresponse *r = (nshttpurlresponse *)task.response;         if ([r statuscode] == 200) {             self.returneddata = responseobject;             nsstring *authcode = [self.returneddata authcode];             nslog(@"authcode: %@", authcode);             returnstring = authcode;         }     } } failure:^(nsurlsessiondatatask *task, nserror *error) {     uialertview *alertview = [[uialertview alloc] initwithtitle:@"error" message:[error localizeddescription] delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil];     [alertview show]; }];  //this returns "nothing yet!" return returnstring; 

}

an asynchronous method won't able return things would. end of method reached before asynchronous call has completed. should use callback block string out. in success block run callback block, passing in string. want pass callback block method you'll need rename (void)getauthcodewithcallback:(callbackblock)callback.

here's callback block typedef started. typedef void(^callbackblock)(id object);


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -