2016-07-28 77 views
1

我有一個NSMutableArray的這樣的創建刪除的對象..如何從NSMutableArray的是hasSufix

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年7月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

+0

'[urlArray filterUsingPredicate:[NSPredicate predicateWithFormat: 「JPG」 @ 「SELF的endsWith並[c]%@」,@]'?而且'urlArray'的alloc/init是無用的,因爲你剛分配它。 – Larme

+0

我試過這個「[urlArray filterUsingPredicate:[NSPredicate predicateWithFormat:@」SELF ENDSWITH [c]%@「,@」。jpg「]」,它刪除數組的所有內容。 –

+0

這是因爲看起來每個URL末尾都有一個額外的字符。而且我讓謂詞只用.jpg保留,它應該有一個NOT。 – Larme

回答

1

通常存在,在任何在Objective-C中,這表現爲Collection XXX was mutated while being enumerated異常,我很驚訝你沒有得到這個異常(這可能意味着你的測試失敗了,並且沒有企圖正在發瘋e從數組中刪除一個條目)。

編輯我相信你的URL字符串在最後有一個無關的換行符,這就是爲什麼你沒有打這個異常)。

有幾個接受的解決方法:

  1. 迭代向後通過陣列。
  2. 保留一個條目列表,以便在最後刪除並刪除它們。

我通常使用後一種方法:

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

Upvoted for good answer – user3182143

+0

我也試過這個NSMutableArray * toRemove = [NSMutableArray new]; for(NSString * url in urlArray) if([url hasSuffix:stringToRemove] [toRemove addObject:url]; [urlArray removeObjectsInArray:toRemove];沒有成功 –

+0

@JohnZ你讀過** EDIT **位了嗎? – Droppy