ios:使用NSURLRequest返回值的方法

时间:2014-08-12 15:10:37

标签: ios objective-c

我有一个方法向api发出特定请求,我想从中返回结果。这是我的方法:

-(NSDictionary *) getProfileDataForUser:(NSString *)user_id {
NSURL *getProfileDataRequestURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@users/profile.php?user_id=%@", _apiRootUrl, user_id]];
NSMutableURLRequest *getProfileDataRequest = [NSMutableURLRequest requestWithURL:getProfileDataRequestURL cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0f];
[getProfileDataRequest setHTTPMethod:@"GET"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:getProfileDataRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    error = nil;
    id jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
    if (jsonData != nil && error == nil) {
        NSDictionary *responseDict = jsonData;
        if ([responseDict objectForKey:@"user"]) {
            // this is a dictionary
            return [responseDict objectForKey:@"user"];
        } else {
            return responseDict;
        }
    }
    else {
        return @{};
    }
}];

}

问题是我遇到了多个语义问题:

sendAsynchronousRequest方法错误:

Incompatible block pointer types sending 'id (^)(NSURLResponse *__strong, NSData *__strong, NSError *__strong)' to parameter of type 'void (^)(NSURLResponse *__strong, NSData *__strong, NSError *__strong)'

退货错误:

Return type 'NSDictionary *' must match previous return type 'id' when block literal has unspecified explicit return type

我尝试将我的方法的返回类型更改为id,然后返回jsonData但我仍然遇到第一个语义问题。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:2)

url请求是异步的,因此该方法需要使用块/回调来返回结果:

- (void) getProfileDataForUser:(NSString *)user_id withCallback:(void (^)(NSDictionary *jsonData))callback {
    NSURL *getProfileDataRequestURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@users/profile.php?user_id=%@", _apiRootUrl, user_id]];
    NSMutableURLRequest *getProfileDataRequest = [NSMutableURLRequest requestWithURL:getProfileDataRequestURL cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0f];
    [getProfileDataRequest setHTTPMethod:@"GET"];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [NSURLConnection sendAsynchronousRequest:getProfileDataRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        error = nil;
        id jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
        callback(jsonData);
    }];
}

答案 1 :(得分:0)

在您的区块中,删除return语句

完成块不应该返回任何内容,这就是为什么你会出现语义问题

Incompatible: Sending 'id (^)(NSURLResponse....' to parameter of type 'void (^)(NSURLResponse...'
                        ^                                                ^
                        |-----------------is different from--------------|

编辑:

如果要从完成处理程序重构NSDictionary重构代码,请执行以下操作:

-(NSDictionary *) getProfileDataForUser:(NSString *)user_id {
    NSURL *getProfileDataRequestURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@users/profile.php?user_id=%@", _apiRootUrl, user_id]];
    NSMutableURLRequest *getProfileDataRequest = [NSMutableURLRequest requestWithURL:getProfileDataRequestURL cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0f];
    [getProfileDataRequest setHTTPMethod:@"GET"];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    // This holds the response to be returned from this method
    __block NSDictionary *rDict = nil;

    [NSURLConnection sendSynchronousRequest:getProfileDataRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        error = nil;
        id jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
        if (jsonData != nil && error == nil) {
            NSDictionary *responseDict = jsonData;
            if ([responseDict objectForKey:@"user"]) {
                // this is a dictionary
                rDict = [responseDict objectForKey:@"user"];
            } else {
                rDict = responseDict;
            }
        }
        else {
            rDict = @{};
        }
    }];

    return rDict;
}

修改

以上代码仅在synchronous请求时才有效。