iOS how get variable value from block? -


can variable value block?

    - (params *) getparams {     __block params* params = nil;         //make post, requests     [jsonhttpclient getjsonfromurlwithstring:@"http://www.blankspot.ru/api/getinterval"                                        params:nil                                       completion:^(id json, jsonmodelerror *err) {                                        nslog(@"json = %@", json);                                        nslog(@"error = %@", err );                                       nsdictionary* json1 = json;                                        nslog(@" %@ ob ", [json1 objectforkey:@"success"]);                                        params = [[params alloc] initwithdictionary:json1 error:&err];                                      nslog(@"params123 = %@", params); // not null                                         }];      nslog(@"params123 = %@", params); //this null     return params;    } 

in first variant nslog view non null value, second variant after block nil.

this because getjsonfromurlwithstring asynchronous (it uses dispatch_async make request call) means called on thread while current thread keep running.

your nslog shows nil since block executed time after nslog line reached due asynchronous nature of call (as url requests take time complete , not immediate)


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -