如何从NSMutableArray中删除hasSufix中的对象

时间:2016-07-28 14:04:04

标签: objective-c xcode nsmutablearray

我有一个像这样创建的NSMutableArray ..

    NSString *urlString = [NSString stringWithFormat:@"%@", @"http://192.168.43.1:8080/mediaListFull"];
    NSURL *stringURL = [NSURL URLWithString:urlString];
    NSData *urlData = [NSData dataWithContentsOfURL:stringURL];
    NSString *stringFromData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];


/// Populating the urlArray "tableView" from stringFromData..
    urlArray = [[NSMutableArray alloc]init];
    urlArray = [[stringFromData componentsSeparatedByString:@"\n"]mutableCopy];

我想删除所有带后缀.jpg的字符串。我正在使用此代码尝试完成此操作,但它无效。

 NSString *stringToRemove = @".jpg";
for (int i = 0; i < urlArray.count; i ++) {
     NSLog(@"The UrlArray has this in it %@",[urlArray objectAtIndex:i]);
    NSString *temp = [NSString stringWithFormat:@"%@",[urlArray objectAtIndex:i]];
    if ([temp hasSuffix:stringToRemove]) {
        [urlArray removeObjectAtIndex:i];
    }

}

因此,当我记录数组时,它仍然包含所有.jpg文件。 任何帮助非常感谢。

FYI the array looks like this. 

2016-07-28 10:00:22.087 ImageURLDownLoad [716:232095] urlArray中有此内容(

 "http://192.168.43.1:8080/mediaResponse/?fileName=/storage/emulated/0/liveMedia/LivePic-19700101_001527.jpg
",
    "http://192.168.43.1:8080/mediaResponse/?fileName=/storage/emulated/0/liveMedia/LivePic-19700101_001533.jpg
",
    "http://192.168.43.1:8080/mediaResponse/?fileName=/storage/emulated/0/liveMedia/LivePic-19700101_000600.jpg
",
    "http://192.168.43.1:8080/mediaResponse/?fileName=/storage/emulated/0/liveMedia/LivePic-19700101_000605.jpg
",
    "http://192.168.43.1:8080/mediaResponse/?fileName=/storage/emulated/0/liveMedia/LivePic-19700101_000610.jpg
",
    "http://192.168.43.1:8080/mediaResponse/?fileName=/storage/emulated/0/liveMedia/LivePic-19700101_000632.jpg
",
    "http://192.168.43.1:8080/mediaResponse/?fileName=/storage/emulated/0/liveMedia/LivePic-19700101_000642.jpg
",
    "http://192.168.43.1:8080/mediaResponse/?fileName=/storage/emulated/0/liveMedia/LivePic-19700101_000652.jpg
",
    "http://192.168.43.1:8080/mediaResponse/?fileName=/storage/emulated/0/liveMedia/LiveVid-19700101_000544.mp4
",
    "http://192.168.43.1:8080/mediaResponse/?fileName=/storage/emulated/0/liveMedia/LiveVid-19700101_001101.mp4
",
    "http://192.168.43.1:8080/mediaResponse/?fileName=/storage/emulated/0/liveMedia/LiveVid-19700101_001129.mp4
",
    "http://192.168.43.1:8080/mediaResponse/?fileName=/storage/emulated/0/liveMedia/LiveVid-19700101_001429.mp4
",
    "http://192.168.43.1:8080/mediaResponse/?fileName=/storage/emulated/0/liveMedia/LivePic-19700101_004538.mp4"
)

此致 JZ

1 个答案:

答案 0 :(得分:1)

在任何语言/库中,通常存在与循环数组和修改该循环内的数组相关的问题。在Objective-C中,这表现为Collection XXX was mutated while being enumerated例外。我很惊讶你没有得到这个异常(这可能意味着你的测试失败并且没有尝试从数组中删除一个条目)。

编辑我相信您的网址字符串最后会有一个无关的换行符,这就是您没有遇到此异常的原因。)

有几种可接受的解决方法:

  1. 向后遍历数组。
  2. 保留一个条目列表,以便最后删除和删除它们。
  3. 我通常使用后一种方法:

    NSMutableArray *toRemove = [NSMutableArray new];
    for (NSString *url in urlArray)
        if ([url hasSuffix:stringToRemove])
            [toRemove addObject:url];
    [urlArray removeObjectsInArray:toRemove];