Hi I’m new to using blocks in Objective-C
What I think I want is the following:
- (void) iNeedAToken {
NSString *token = [self theMethodThatShouldReturnTheToken];
}
- (NSString) theMethodThatShouldReturnTheToken {
[myAwesomeAsyncMethod success:^(id JSON) {
NSString *token = [JSON objectForKey:@"FOO"];
return token;
}]
}
Is this possible? Or is this the wrong logic all together?
Thanks!
You’re mixing async with synchronous code. You’re
theMethodThatShouldReturnTheTokenalready returned (you’re missing a return value) before the block passed tosuccessfinishes.Best bet would be to continue your process from the success block.
You start by calling
requestToken. This will start the async request for your token. Some time might pass, but eventuallydoSomethingWithTokenwill be called where you can use the received token.