如何将 return PINCode 的位置数组作为带有完成代码块的输入参数?
How to return Array of locations for the PINCode as an input parameter with completion block of code?
我有一个场景,用户将输入 PINCode 号码,我必须将该号码作为参数传递给 Web 服务 API(用户是一个单身人士 class),然后我将得到一个数组作为响应的位置,我应该在 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) 我必须创建 Class 方法还是实例方法?我必须在哪里为这些情况创建 class 和实例方法?
也许这对你有帮助:
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"];
}
}
我有一个场景,用户将输入 PINCode 号码,我必须将该号码作为参数传递给 Web 服务 API(用户是一个单身人士 class),然后我将得到一个数组作为响应的位置,我应该在 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) 我必须创建 Class 方法还是实例方法?我必须在哪里为这些情况创建 class 和实例方法?
也许这对你有帮助:
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"];
}
}