内部块的返回值(Objective-C) [英] Return value from inside block (Objective-C)
问题描述
我一直试图从里面块中获取一个值几个小时,我无法理解如何在完成时使用处理程序以及几乎所有内容。
这是我的代码:
I've been trying to get a value from inside a block for a few hours now, I can't understand how to use the handlers on completion and literally everything.
Here's my code:
+ (void)downloadUserID:(void(^)(NSString *result))handler {
//Now redirect to assignments page
__block NSMutableString *returnString = [[NSMutableString alloc] init]; //'__block' so that it has a direct connection to both scopes, in the method AND in the block
NSURL *homeURL = [NSURL URLWithString:@"https://mistar.oakland.k12.mi.us/novi/StudentPortal/Home/PortalMainPage"];
NSMutableURLRequest *requestHome = [[NSMutableURLRequest alloc] initWithURL:homeURL];
[requestHome setHTTPMethod:@"GET"]; // this looks like GET request, not POST
[NSURLConnection sendAsynchronousRequest:requestHome queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *homeResponse, NSData *homeData, NSError *homeError) {
// do whatever with the data...and errors
if ([homeData length] > 0 && homeError == nil) {
NSError *parseError;
NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:homeData options:0 error:&parseError];
if (responseJSON) {
// the response was JSON and we successfully decoded it
//NSLog(@"Response was = %@", responseJSON);
} else {
// the response was not JSON, so let's see what it was so we can diagnose the issue
returnString = (@"Response was not JSON (from home), it was = %@", [[NSMutableString alloc] initWithData:homeData encoding:NSUTF8StringEncoding]);
//NSLog(returnString);
}
}
else {
//NSLog(@"error: %@", homeError);
}
}];
//NSLog(@"myResult: %@", [[NSString alloc] initWithData:myResult encoding:NSUTF8StringEncoding]);
handler(returnString);
}
- (void)getUserID {
[TClient downloadUserID:^(NSString *getIt){
NSLog([NSString stringWithFormat:@"From get userID %@", getIt]);
}];
}
所以我试图NSLog returnString
来自 downloadUserID
方法。
我首先尝试返回,然后我意识到你无法从一个区块内返回。所以现在我一直试图用 :( void(^)(NSString * result))处理程序
来尝试从另一个类方法访问它。
So I'm trying to NSLog the returnString
from the downloadUserID
method.
I first tried returning, then I realized you can't do a return from inside a block. So now I've been trying to do it with the :(void(^)(NSString *result))handler
to try and access it from another class method.
所以我从 getUserID
方法调用 downloadUserID
,并尝试记录 returnString
字符串。它只是一无所获。它只打印来自get userID
而没有别的。
So I'm calling downloadUserID
from the getUserID
method, and trying to log the returnString
string. It just keeps going to nil. It just prints From get userID
and nothing else.
如何访问 returnString
那是在 downloadUserID
方法的块内?
How do I access the returnString
that's inside the block of the downloadUserID
method?
推荐答案
问题不在于块
本身,问题在于意识到该块是异步执行的。
The problem is not the block
itself, the problem is realizing that the block is executed asynchronously.
在您的代码中,当您调用 handler(returnString);
该块可能仍在另一个线程上执行时,所以此时无法捕获该值。
In your code, at the time you call handler(returnString);
the block is probably still executing on another thread, so there's no way you can catch the value at this point.
你想要做的就是在阻挡区内移动该线(可能在结束括号之前的末尾)。
Probably what you want to do is move that line inside the block (probably at the end, before the closing braces).
这篇关于内部块的返回值(Objective-C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!