1

我有一个场景,用户将输入 PINCode 号码,我必须将该号码作为参数传递给 Webservices API(用户是一个单例类),然后我将获得一个位置数组作为响应,并且我应该在 A lable 中显示该响应在 PINCode 的文本字段下方。所以我的理解是我必须在该方法中创建一个带有完成块的方法。但我的疑问是如何让位置数组在标签中表示。我真的很困惑,我试过这个

API.h

+(void)submitPINCode:(NSInteger *)pinCode CompletionBlock:(void(^)(BOOL status, NSArray * responseArray))handler;

API.m

+(void)submitPINCode:(NSInteger *)pinCode CompletionBlock:(void(^)(BOOL, NSArray *))handler

{

    NSString *string = [NSString stringWithFormat:@"%@%@",URL,pinCode];
    NSURL *url = [NSURL URLWithString:string];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];


    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    operation.responseSerializer = [AFJSONResponseSerializer serializer];

    operation.responseSerializer = [AFXMLParserResponseSerializer serializer];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        BOOL status = NO;
        if (operation.response.statusCode == kStatusCodeSuccessfullResponse) {
            status = YES;
            //  here we have to send an array so make the response object in to array or create a response Dictionary as you wish

            NSArray *responseArray = (NSDictionary *)responseObject;

        }

        handler(status,BfErrorCodeUnknownError, responseArray);


    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSInteger statusCode = operation.response.statusCode;
        BfErrorCode errorCode = BfErrorCodeUnknownError;
        if (statusCode == kStatusCodeNoInternetConnection) {
            errorCode = BfErrorCodeNetworkError;
        }

        NSString *errorMessage = [NSString stringWithBfErrorCode:errorCode];

        handler(NO,errorCode,nil); // here if nil is doesnt work just pass an empty array instead of nil create an array and pass it
    }];

//    [operation start];

}

所以我的疑问是:

1)我是在以正确的方式前进,或者我必须做哪些改变才能得到答案,并且

2)天气我必须创建类方法或实例方法?对于这些情况,我必须在哪里创建类和实例方法?

4

1 回答 1

2

可能这可以帮助你:

API.h

typedef NS_ENUM (NSInteger, WebServiceResult)
{
    WebServiceResultSuccess = 0,
    WebServiceResultFail,
    WebServiceResultError
};
typedef void(^myCompletionA)(NSDictionary *data,WebServiceResult result);
+(void)postDataDict:(NSInteger *)pinCode isLoding:(BOOL)loading WithBlock:(myCompletionA)compileBlock;

API.m

+(void)postDataDict:(NSInteger *)pinCode isLoding:(BOOL)loading WithBlock:(myCompletionA)compileBlock{
    if ([objAppDelegate connected]) {
        if (loading) {
            [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeNone];
        }

        NSString *urlString=[[NSString stringWithFormat:@"%@%@",BASE_URL,pinCode] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
       // manager.requestSerializer = [AFJSONResponseSerializer serializer];
        //manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
        [manager POST:urlString parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [SVProgressHUD dismiss];
//            NSLog(@"Response : %@",responseObject);
            if ([responseObject[@"status"] isEqualToString:@"success"])
            {
                compileBlock(responseObject,WebServiceResultSuccess);
            }
            else
            {
                compileBlock(responseObject,WebServiceResultFail);
            }

        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSDictionary *errorDict=[[NSDictionary alloc]initWithObjectsAndKeys:error,@"error", nil];
            compileBlock(errorDict,WebServiceResultError);
            NSLog(@"%@",error.description);
            [SVProgressHUD dismiss];
        } ];
    }
    else
    {
        [objAppDelegate showMessage:@"No internet connection." withTitle:@"nil"];
    }
}
于 2015-12-03T13:39:30.213 回答