如何分离NSMutablearray中存在的元素

时间:2014-02-07 13:34:11

标签: ios nsmutablearray

我有一个NSMutablearray的应用程序显示如下。我想要一个接一个包含所有城市而没有NO CITY的另一个数组。截至目前,索引1处的对象显示:

 2014-02-07 18:41:15.567 [313:a0b] cities Array (
                                            "Bangalore",
                                            "Chennai",
                                            "Delhi",
                                            "Mumbai"
                                            )

我希望将其分隔为:

Bangalore
Chennai
Delhi
Mumbai
Pune with out NO CITY


2014-02-07 18:41:15.567 [313:a0b] cities Array  NO CITY
2014-02-07 18:41:15.567 [313:a0b] cities Array (
"Bangalore",
"Chennai",
"Delhi",
"Mumbai"
)
2014-02-07 18:41:15.567 [313:a0b] functionalities Array NO CITY
2014-02-07 18:41:15.568 [313:a0b] functionalities Array Pune

1 个答案:

答案 0 :(得分:0)

请找到解决方案:

    NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:@"Bangalore",@"Chennai", @"Delhi", @"Pune", nil];
    NSLog(@"%@", [arr description]);

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

    for (NSString *str in arr) {
        if (![str isEqualToString:@"Pune"]) {
            [arr1 insertObject:str atIndex:arr1.count];
        }
    }
    NSLog(@"%@", [arr1 description]);

如果要在同一个数组中执行,请使用以下代码:

    NSMutableArray *arr = [[NSMutableArray alloc] init];
    [arr addObject:@"Banagalore"];
    [arr addObject:@"Delhi"];
    [arr addObject:@"Mumbai"];
    [arr addObject:@"Pune"];

    NSLog(@"%@", [arr description]);

    for (int i = 0; i <[arr count] ; i++ )
    {
        if ([[arr objectAtIndex:i] isEqualToString:@"Pune"]) {
            [arr removeObjectAtIndex:i];
        }
    }
    NSLog(@"%@", [arr description]);