在Objective C的函数中从完成块返回对象

时间:2016-12-28 07:08:21

标签: ios objective-c json azure

我有一个调用azure REST API的函数。

    - (void)authenticateUser:(NSString*)usernameString
passwordString: (NSString*)passwordString{
    self.client = [MSClient clientWithApplicationURLString:@"url" applicationKey:@"url"];

    [self.client invokeAPI:@"AuthenticateAndFetchData"
                      body:nil
                HTTPMethod:@"GET"
                parameters:@{ @"userid": usernameString, @"password" : passwordString }
                   headers:nil
                completion: ^(NSData *result, NSHTTPURLResponse *response, NSError *error) {
                    if(error) {
                        NSLog(@"ERROR %@", error);
                    } else {

                        NSLog(@"%@", result);
                    }
                }];

}

此代码位于函数内部。我希望将result对象中收到的JSON对象作为NSData返回,以便我可以在其他类中使用此对象并解析数据。任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分:1)

    - (void)authenticateUser:(NSString*)usernameString
passwordString: (NSString*)passwordString completeBlock:(void(^)(NSData *)) completeBlock
    {
        [self.client invokeAPI:@"fetchdata"
                          body:nil
                    HTTPMethod:@"GET"
                    parameters:@{ @"userid": usernameString, @"password" : passwordString }
                       headers:nil
                    completion: ^(NSData *result, NSHTTPURLResponse *response, NSError *error) {
                        if(error) {
                            NSLog(@"ERROR %@", error);
                        } else {
                            printf("%s", result);

                            if(completeBlock)
                                completeBlock(result);

                        }
                    }];
    }

这样打电话:

[self authenticateUser: @"username" passwordString: @"password" completeBlock:^(NSData * data){
        //do something here
    }];

你想要这个吗?