Objective c 块内函数的返回值

Objective c 块内函数的返回值,objective-c,block,afnetworking,Objective C,Block,Afnetworking,我正在使用AFNetworking从服务器获取数据: -(NSArray)some function { AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success: ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

我正在使用AFNetworking从服务器获取数据:

-(NSArray)some function {
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
        success: ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
            NSArray *jsonArray =[JSON valueForKey:@"posts"];
        }
        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {}
}

所以我在这里要做的是将jsonArray返回给函数。显然,返回不起作用。

不能使用完成块为方法创建返回值。
AFJSONRequestOperation
异步工作
someFunction
将在操作仍在运行时返回。成功和失败模块是如何在需要的地方获得结果值

这里的一个选项是将调用者作为参数传递给包装器方法,以便完成块可以传递数组

- (void)goFetch:(id)caller
{
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
    success: ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

        [caller takeThisArrayAndShoveIt:[JSON valueForKey:@"posts"]];
    }
    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {}
}
您还可以让调用方创建并传递一个块,以便在成功时运行。然后
goFetch:
不再需要知道调用者上存在哪些属性

- (void)goFetch:(void(^)(NSArray *))completion
{
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
    success: ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        if( completion ) completion([JSON valueForKey:@"posts"]);
    }
    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {}
}

正如其他人所说,在处理异步调用时不能这样做。您可以将完成块作为参数传递,而不是返回预期的数组

typedef void (^Completion)(NSArray* array, NSError *error);

-(void)someFunctionWithBlock:(Completion)block {
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
        success: ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
            NSArray *jsonArray =[JSON valueForKey:@"posts"];
            if (block) block(jsonArray, nil);
        }
        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
            if (block) block(nil, error); 
        }
}
然后你把它叫做someFunction。此代码还将为您执行正确的错误处理

[yourClassInstance someFunctionWithBlock:^(NSArray* array, NSError *error) {
   if (error) {
      NSLog(%@"Oops error: %@",error.localizedDescription);
   } else {
      //do what you want with the returned array here.
   }
}];

我面对这种问题,用下面的方法解决它。 我用方块看到了上面的答案。但这个解决方案在当时更适合。 该方法逻辑简单。您需要将对象及其方法作为参数发送,请求完成后将调用该方法。 希望能有帮助

+(void)request:(NSString *)link parameters:(NSDictionary *)params  forInstance:(id)instance returns:(SEL)returnValue
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:link
      parameters:params
         success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         [instance performSelector:returnValue withObject: responseObject];
     }
         failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         [instance performSelector:returnValue withObject:nil];
         //NSLog(@"Error: %@", error);
     }];
}

你不能这样做。由于您处理的是异步调用,因此
somefunction
方法将在返回值之前很久返回。我建议您后退一步,考虑一下代码的作用。您很快就会意识到,您想要的东西没有意义。您可以将块传递给函数,函数将在成功或错误时执行。