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
Post a Comment