我無法從我的NSMutableArray
中刪除項目。我對Objective-C非常陌生,所以請耐心等待。枚舉並從NSMutableArray中刪除特定對象
到目前爲止,我有以下幾點:我試圖從數組中刪除一行,如果它裏面有某些文本。在快速枚舉時我無法做到這一點,所以我試圖存儲索引,在枚舉完成後刪除。但是,我被告知這使得從一個整數的指針沒有演員。困惑!
//First remove any previous Offending entry.
//Read hostfile into array.
NSString *hostFileOriginalString = [[NSString alloc] initWithContentsOfFile:@"/etc/hosts"];
NSMutableArray *hostFileOriginalArray = [[hostFileOriginalString componentsSeparatedByString:@"\n"] mutableCopy];
NSInteger hostFileOffendingLocation;
//Use a for each loop to iterate through the array.
for (NSString *lineOfHosts in hostFileOriginalArray) {
if ([lineOfHosts rangeOfString:@"Offending"].location != NSNotFound) {
//Offending entry found, so remove it.
//[hostFileOriginalArray removeObject:lineOfHosts];
hostFileOffendingLocation = [hostFileOriginalArray indexOfObject:lineOfHosts];
//NSLog(@"%@", hostFileOffendingLocation);
}
}
//Release the Array.
[hostFileOriginalArray release];
//Remove offending entry from Array.
[hostFileOriginalArray removeObject:hostFileOffendingLocation];
這使得一個沒有強制轉換的整型指針,因爲你正在向'removeObject:'傳遞一個整數。我認爲'removeObjectAtIndex:'就是你以後的樣子。另外,爲什麼在嘗試刪除對象之前釋放數組? – albertamg 2011-05-06 19:12:08