gpt4 book ai didi

ios - 如何使用完成代码块将 PINCode 的位置数组作为输入参数返回?

转载 作者:行者123 更新时间:2023-11-29 10:22:42 26 4
gpt4 key购买 nike

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

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) 我必须创建类方法还是实例方法?通常我必须在哪里为这些情况创建类和实例方法?

最佳答案

也许这对你有帮助:

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"];
}
}

关于ios - 如何使用完成代码块将 PINCode 的位置数组作为输入参数返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34067168/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com