存储对象集合的最佳方式

时间:2015-03-14 20:47:10

标签: objective-c

我有几个要存储在数组中的对象。在objective-c中执行此操作的方法是什么?我一直在研究NSMutableDictionary,但没有运气。

在下面的代码中,我正在遍历一个字符串数组,为每个字符串进行网络调用,并为每个字符串返回一个对象。我想将我的对象存储在稍后可以访问的列表中。

-(void) setPictures {

    for(id item in self.phraseWordsArray) {

    AFNetworkingAPIClient *netowrkingObject = [[AFNetworkingAPIClient alloc] init];
    [netowrkingObject getPhotoForWord:item success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"responseObject=%@",responseObject);
        self.test = responseObject;
        [self printOutJSON];



    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error=%@",error);
    }];
    }   
}

2 个答案:

答案 0 :(得分:2)

看起来NSMutableArray是您需要的,我是否正确?

答案 1 :(得分:1)

您可以将响应对象存储到NSMutableArray中,该// In your interface... @property (nonatomic,strong) NSMutableArray *responseObjects; // ... Later in code self.responseObjects = [NSMutableArray array]; - (void)setPictures { for(id item in self.phraseWordsArray) { AFNetworkingAPIClient *netowrkingObject = [[AFNetworkingAPIClient alloc] init]; [netowrkingObject getPhotoForWord:item success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"responseObject=%@",responseObject); self.test = responseObject; [self printOutJSON]; if (responseObject) { [self.responseObjects addObject:responseObject]; } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error=%@",error); }]; } } 是UIViewController上调用此代码或其他地方的属性。有关示例方案,请参阅以下内容:

{{1}}