如何在成功块、iOS、Objective-C中返回NSArray
这是我的密码。在将数据从web服务分配到数组后,我想返回一个包含数据的数组。我需要使用块进行检查。 这是我的头文件如何在成功块、iOS、Objective-C中返回NSArray,ios,objective-c,block,afnetworking-3,Ios,Objective C,Block,Afnetworking 3,这是我的密码。在将数据从web服务分配到数组后,我想返回一个包含数据的数组。我需要使用块进行检查。 这是我的头文件 typedef void(^FailureBlock)(NSError *error); typedef void(^SuccessBlock) (NSMutableArray *responseArray); 这是我的实现文件 - (void)setupConnectionWithsuccess:(SuccessBlock)success failure:(FailureBlo
typedef void(^FailureBlock)(NSError *error);
typedef void(^SuccessBlock) (NSMutableArray *responseArray);
这是我的实现文件
- (void)setupConnectionWithsuccess:(SuccessBlock)success failure:(FailureBlock)failure
{
airportArray = nil;
NSString *airportCode = [NSString stringWithFormat:@"some code"];
NSString *authenticationCode = [NSString stringWithFormat:@"some api"];
NSString *baseurl = [NSString stringWithFormat:@"some url",authenticationCode,airportCode];
// NSString *mainurlString = [NSString stringWithFormat:@""];
// NSURL *mainurl = [NSURL URLWithString:mainurlString];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:baseurl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSArray *mainArray = (NSArray *)responseObject;
airportArray = [[NSMutableArray alloc] init];
for (NSDictionary *all in mainArray) {
airports = [all objectForKey:@"Airport"];
[airportArray addObject:airports];
NSLog(@"%@", airports);
}
if(success){
success(airportArray);
}
//NSLog(@"%@", responseObject);
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
UIAlertController *mainAlert = [UIAlertController alertControllerWithTitle:@"Something Wrong!" message:[error localizedDescription] preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:mainAlert animated:YES completion:nil];
}];
}
成功后,我想从这个方法返回数组。我怎么做
- (NSArray *)returnAll
{
[self setupConnectionWithsuccess:^(NSMutableArray *responseArray) {
} failure:^(NSError *error) {
}];
}
请帮我解决这个问题。我是iOS块的新手。尝试如下我使用id,因为在id中你可以传递任何对象
- (void)setupConnectionWithsuccess:(void (^)(id responseObject, NSError *error))completion{
airportArray = nil;
NSString *airportCode = [NSString stringWithFormat:@"some code"];
NSString *authenticationCode = [NSString stringWithFormat:@"some api"];
NSString *baseurl = [NSString stringWithFormat:@"some url",authenticationCode,airportCode];
// NSString *mainurlString = [NSString stringWithFormat:@""];
// NSURL *mainurl = [NSURL URLWithString:mainurlString];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:baseurl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSArray *mainArray = (NSArray *)responseObject;
airportArray = [[NSMutableArray alloc] init];
for (NSDictionary *all in mainArray) {
airports = [all objectForKey:@"Airport"];
[airportArray addObject:airports];
NSLog(@"%@", airports);
}
if (completion)
completion(responseObject,nil);
//NSLog(@"%@", responseObject);
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (completion)
completion(nil,error);
UIAlertController *mainAlert = [UIAlertController alertControllerWithTitle:@"Something Wrong!" message:[error localizedDescription] preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:mainAlert animated:YES completion:nil];
}];}
那就叫它
[self setupConnectionWithsuccess:^(id responseObject, NSError *error) {
if (responseObject) {
NSLog(@"responsceObect");
}
}];
当函数返回时,它什么也得不到。因为您的块以异步方式执行。您应该修改函数以处理块中的阵列数据。您遇到了什么问题?无需检查是否(成功)和是否(失败)因为这两个块是不同的。您无法返回块中的任何内容,而必须在调用此[self-setupConnectionWithsuccess:^(NSMutableArray*responseArray){您在此处获得nsmutable数组}失败:^(NSError*error)时在代码中调用此处接收响应时使用该块{}];我希望在加载数据后返回数组。因此我使用returnAll方法。在她中,我希望返回responseArray。我声明了一个公共可变数组,并将其分配给Response array的值(在returnAll方法中)然后我返回它。然后我在viewdidload方法中打印它。但是它打印nullu不能从块中返回数组,因为无论何时返回任何对象,它都会在获得成功或失败之前返回,所以下次它不会返回任何内容