如何在块内部分配变量?

时间:2013-11-07 12:02:44

标签: ios objective-c-blocks nsurlsession

从其他问题来看,似乎所有我要做的就是在我的变量前添加__block,但是,它似乎对我没用。

当在块内时,使用NSLog()检查时正确分配了令牌。如果我在返回令牌之前再次检查它;它变成了NULL。

- (NSString *)extractTokenFromURL:(NSURL *)tokenURL
{
__block NSString *token = nil;

NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
                                                      delegate:self
                                                 delegateQueue:nil];
[[session dataTaskWithURL:self.tokenURL
        completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    if (!error) {
        NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
        if (httpResp.statusCode == 200) {
            NSString *content = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSRange divRange = [content rangeOfString:@"<div id='token' style='display:none;'>" options:NSCaseInsensitiveSearch];
            if (divRange.location != NSNotFound) {
                NSRange endDivRange;
                endDivRange.location = divRange.length + divRange.location;
                endDivRange.length   = [content length] - endDivRange.location;
                endDivRange = [content rangeOfString:@"</div>" options:NSCaseInsensitiveSearch range:endDivRange];

                if (endDivRange.location != NSNotFound) {
                    divRange.location += divRange.length;
                    divRange.length  = endDivRange.location - divRange.location;

                    dispatch_async(dispatch_get_main_queue(), ^{
                        token = [content substringWithRange:divRange];
                    });
                }
            }
        }
    }
}] resume];

return token;
}

1 个答案:

答案 0 :(得分:2)

这是因为任务是异步运行的,因此该方法将在token被分配之前返回。

您应该做的是将完成块传递给此方法,并在获得令牌后调用它。这样,在执行请求时不会阻塞主线程。

你可以这样做:

- (void)extractTokenFromURL:(NSURL *)tokenURL completion:(void (^)(NSString *token, NSError* error))completion {
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
    [[session dataTaskWithURL:self.tokenURL
            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (!error) {
            NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
            if (httpResp.statusCode == 200) {
                NSString *token = // ...;
                if (token) { // we have a valid token
                    if (completion) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            completion(token, nil);
                        });
                    } 
                } else if (completion) {
                    // create an error indicating why the token is not valid
                    NSError *error = // ...;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        completion(nil, error);
                    });
                }
            }
        } else if (completion) {
            // send the http error. you could also wrap it on your own error domain and code
            dispatch_async(dispatch_get_main_queue(), ^{
                completion(nil, error);
            });
        }
    }] resume];
}

您还可以拥有两个不同的块:一个用于成功,另一个用于失败。

相关问题