1

我是 tableViews 和字典的新手,我遇到了问题!在 ViewDidLoad 中,我正在初始化许多 MutableArray,并且正在使用 NSDictionary 添加数据。例子:

- (void)viewDidLoad {
nomosXiou=[[NSMutableArray alloc] init];

[nomosXiou addObject:[[NSDictionary alloc] initWithObjectsAndKeys:@"Mary",@"name",@"USA",@"country", nil]];
[nomosXiou addObject:[[NSDictionary alloc] initWithObjectsAndKeys:@"Peter",@"name",@"Germany",@"country", nil]]; 

[super viewDidLoad];
// Do any additional setup after loading the view.}

在之前的 ViewController 中,用户选择了 Country。基于该选择,我如何从我的数组中删除所有其他条目???

提前致谢...

4

2 回答 2

2

首先请注意,您的代码片段有错误。它应该是:

NSMutableArray *nomosXiou= [[NSMutableArray alloc] init];

有很多方法可以做你想做的事,但最直接的可能是以下几种:

NSString *countryName;    // You picked this in another view controller
NSMutableArray *newNomosXiou= [[NSMutableArray alloc] init];

for (NSDictionary *entry in nomosXiou) {
    if ([[entry objectForKey:@"country"] isEqualToString:countryName])
        [newNomosXiou addObject:entry];
}

完成后将仅包含来自 中设置的国家/地区newNomosXiou的条目。nomosXioucountryName

于 2012-07-28T21:27:59.897 回答
0

像这样的东西可以完成这项工作:

NSMutableArray *nomosXiou = [[NSMutableArray alloc] init];
NSString *country = @"Germany"; // This is what you got from previous controller

// Some test data. Here we will eventually keep only countries == Germany
[nomosXiou addObject:[[NSDictionary alloc] initWithObjectsAndKeys:@"Mary",@"name",@"USA",@"country", nil]];
[nomosXiou addObject:[[NSDictionary alloc] initWithObjectsAndKeys:@"Peter",@"name",@"Germany",@"country", nil]];
[nomosXiou addObject:[[NSDictionary alloc] initWithObjectsAndKeys:@"George",@"name",@"Germany",@"country", nil]];

// Here we'll keep track of all the objects passing our test
// i.e. they are not equal to our 'country' string
NSIndexSet *indexset = [nomosXiou indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop){
    return (BOOL)![[obj valueForKey:@"country"] isEqualToString:country];
    }];

// Finally we remove the objects from our array
[nomosXiou removeObjectsAtIndexes:indexset];
于 2012-07-28T22:46:07.563 回答