我对Objective-C还是很陌生,必须从@property (strong, nonatomic) NSMutableArray *allCategories块中的AFHTTPRequestOperationManager内部动态更改success的值。
在循环中,[self.allCategories addObject:tempObject];不会更改allCategories的值。
该变量已在viewDidLoad中初始化为self.allCategories = [[NSMutableArray alloc]init];

我还尝试在启动__block NSMutableArray *tempCategories = [[NSMutableArray alloc]init];对象之前将临时变量创建为AFHTTPRequestOperationManagertempCategories甚至不保留其值。无法弄清发生了什么.EditSorry给您带来不便viewDidLoad包含以下代码

-(NSMutableArray *)loadData
{
    __block NSMutableArray *tempCategories = [[NSMutableArray alloc]init];

    manager = [AFHTTPRequestOperationManager manager];

    [manager GET:kAPICategoryList
      parameters:nil
         success:^(AFHTTPRequestOperation *operation, id responseObject) {

             // downcast id to NSMutableDictionary
             NSMutableDictionary *json = (NSMutableDictionary *)responseObject;

             // check if dictionary is non nil has at least 1 element
             if (json != nil && [json count] >= 1) {

                 // NSLog(@"json:\t%@", json);

                 // check json is non nil & has success message
                 if ([json objectForKey:kAPIKeyCategoryRoot] != nil) {

                     NSArray *arrCategoriesRoot = [json objectForKey:kAPIKeyCategoryRoot];

                     // check categories has some data
                     if (arrCategoriesRoot.count >= 1) {

                         for (int i = 0; i < arrCategoriesRoot.count; i++) {

                             SomeModel *pCategory;

                             NSDictionary *dctCategorySingle = [arrCategoriesRoot objectAtIndex:i];

                             // check category has sub category
                             if ([dctCategorySingle objectForKey:kAPIKeyCategorySubCategory] != nil) {
                                 // create category with sub category
                                 pCategory = [[SomeModel alloc]initWithSubCategorisedCategoryID:[dctCategorySingle objectForKey:kAPIKeyCategoryID]
                                                                                              name:[dctCategorySingle objectForKey:kAPIKeyCategoryName]
                                                                                             image:kIMGCategoryDefault
                                                                                       subCategory:[dctCategorySingle objectForKey:kAPIKeyCategorySubCategory]];
                             } else{
                                 // create just a category
                                 pCategory = [[SomeModel alloc]initWithCategoryID:[dctCategorySingle objectForKey:kAPIKeyCategoryID]
                                                                                name:[dctCategorySingle objectForKey:kAPIKeyCategoryName]
                                                                               image:kIMGCategoryDefault];
                             } // else just
                             [tempCategories addObject:pCategory];
                             [_allCategories addObject:pCategory];
                         } // for
                         NSLog(@"categories count %lu", [self.allCategories count]);
                     } // if count >= 1
                 }
                 else if ([json objectForKey:kAPIRespMsgCategoryFetchErrKey] != nil) {
                     [Utility showAlertWithTitle:kAPIRespMsgCategoryFetchErrKey
                                         message:[json objectForKey:kAPIRespMsgCategoryFetchErrVal]
                                          button:kMsgButtonOkayTtl];
                 }
             } else {
                 // error in login => enable login
                 NSLog(@"%@", kMsgNetworkEmptyJSON);
             }
         }
     // network error
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"error %@", [error localizedDescription]);
         }];
    NSLog(@"tempCategories count %lu", [tempCategories count]);
    return tempCategories;
}


这是NSLog:2015-03-19 18:27:17.845 MyProject [4011:121268] viewDidLoad的输出形式
2015-03-19 18:27:18.133 MyProject [4011:121268] tempCategories计数0
2015-03-19 18:27:18.136 MyProject [4011:121268] numberOfRowsInSection计数0
2015-03-19 18:27:18.137 MyProject [4011:121268] numberOfRowsInSection计数0
2015-03-19 18:27:19.019 MyProject [4011:121268]当self.allCategories = [[NSMutableArray alloc]init];完成allCategories时,类别计数为20(无)。

最佳答案

据我所知,它应该以这种方式工作..在确定allCategories的内容之前,您确定成功调用了成功块吗?

成功块以异步方式工作,这意味着仅在RequestOperation完成时才执行(如果下载较大的文件可能会花费很长时间)

如果尝试在执行成功块之前获取allCategories的值,则不会获得期望的结果。我建议您在成功块上使用断点或NSLog来查看它是否已在您认为正在执行时执行。

例如

 ...
 successBlock:^(AFHTTPRequestOperation *operation, id responseObject)
 {
     NSLog(@"Success");
     [self.allCategories addObject:tempObject]
  }]; //End of request

[operation start]; //Begin executing the AFHTTPOperation

NSLog("%@",self.allCategories.description); //probably nil or empty
//since the success block hasn't been called yet


编辑:

就像我一样,您正在返回一个由异步操作设置的before值,要从异步操作返回一个值,我建议您看一下answerthis一个。另外,您还应该阅读一下异步任务的工作原理。

基本上,您要对异步操作/任务执行的操作是确保该值在您要使用时可用。这样做的主要问题是您不知道何时设置该值,但是可以确保在设置该值后要执行的操作。

为此,您可以创建一个带有自定义完成块的简单方法

- (void)myCustomMethodWithCompletionBlock: (void (^)(NSArray *))completion {
   //Do your request
   //...
   successBlock:^(AFHTTPRequestOperation *operation, id responseObject)
  {
      NSLog(@"Success");
      completionBlock(allCategories);
   }]; //End of request
}


同时,在您的主要方法中,您调用

 [self myCustomMethodWithCompletionBlock:^(NSArray *allCategories) {
    self.allCategories = allCategories;
    //Do other stuff you need to with that variable since now you are
    //sure the value will be set unless the operation failed
   }];

关于ios - 如何从块内部修改非局部(全局)变量?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29143998/

10-12 21:27