我正在尝试解析我使用 NSTask 执行的 shell 命令的输出。寻找特定的子字符串就足够了,只有永远找不到子字符串。
到目前为止我的代码:
NSPipe *pipe;
pipe = [NSPipe 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);
if([string rangeOfString:@"Connection refused"].location != NSNotFound) {
NSLog(@"found");
} else {
NSLog(@"not found");
}
即使搜索的子字符串包含在变量string中,它也总是打印“未找到” 。提前致谢。