我試圖替換數組「第7行」的第7個索引。 NSMUTABLEARRAY「lines2」源自UNIX命令「ps aux」,我懷疑這個命令返回一個NSCFStrings數組。我現在基本上試圖用「Ss(Running)」代替「Ss」。問題是我每次都得到一個SIGABRT錯誤程序到達它試圖替換特定數組元素的那個部分。我的viewController的代碼如下。無法取代派生自「ps aux」輸出的NSArray UNIX命令
NSLog(@"myString is :%@", myString);
int processID = [myString intValue];
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/ps"];
arguments = [NSArray arrayWithObjects: @"aux", [NSString stringWithFormat:@"%i", processID],nil];
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
//[task setStandardOutput: pipe];
[task setStandardOutput:pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData: data
encoding: NSUTF8StringEncoding];
// NSLog(@"%@",string);
NSArray *lines= [string componentsSeparatedByString:@"\n"];
NSString *lastline = [lines objectAtIndex:[lines count]-2];
// NSLog(@"%@",lastline);
lines2= [lastline componentsSeparatedByString:@" "];
NSLog(@"%@",lines2);
for (int i=0; i<[lines2 count]; i++) {
if([[lines2 objectAtIndex:i] isEqualToString:@""]){
[lines2 removeObjectAtIndex:i];
}
}
for (int i=0; i<[lines2 count]; i++) {
if([[lines2 objectAtIndex:i] isEqualToString:@""]){
[lines2 removeObjectAtIndex:i];
}
}
for (int i=0; i<[lines2 count]; i++) {
if([[lines2 objectAtIndex:7] isEqualToString:@"Ss"]){
[[lines2 objectAtIndex:0] replaceObjectAtIndex:7 withObject:@"SS (Running)"];
}
}
任何幫助非常感謝!
其實它的一個NSMutableArray,我忘了提及。我認爲它拋出一個SIGABRT是因爲我試圖用一個NSString替換一個NSCFString(ps aux的輸出)。 –