我无法从NSMutableArray删除项目。我对Objective-C极为陌生,所以请耐心等待。

到目前为止,我有以下内容:如果行中有某些文本,我正在尝试从数组中删除行。快速枚举时无法执行此操作,因此我试图存储索引,以便在枚举完成后将其删除。但是,有人告诉我,它是使用整数进行指针转换而没有强制转换。困惑!

//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];

最佳答案

我的真正问题是,为什么在修改数组之前先释放数组

尝试移动

[hostFileOriginalArray release];


到之后

[hostFileOriginalArray removeObject:hostFileOffendingLocation];

10-06 13:23