NSTask:进程的启动路径是否存在
该代码用于获取进程的stdout
NSTask * task;
NSPipe * pipe;
NSFileHandle * fileHandle;
task = [ [ NSTask alloc ] init ];
pipe = [ NSPipe pipe ];
fileHandle = [ pipe fileHandleForReading ];
[ task setLaunchPath: @"/usr/bin/lspci" ];
[ task setArguments:[NSArray arrayWithObject:@"-nn"]];
[ task setStandardOutput: pipe ];
[ task setStandardError: pipe ];
[ task launch ];
[ task waitUntilExit];
[ task release];
NSData *outputData = [[pipe fileHandleForReading] readDataToEndOfFile];
NSString *outputString = [[[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding] autorelease];
由于某些系统上不存在/usr/bin/lspci
,因此会发生此致命错误
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“启动路径无法访问”
如何预先检查 lspci
是否存在,如果不存在则向用户显示错误消息?
This code is used to get stdout of the process
NSTask * task;
NSPipe * pipe;
NSFileHandle * fileHandle;
task = [ [ NSTask alloc ] init ];
pipe = [ NSPipe pipe ];
fileHandle = [ pipe fileHandleForReading ];
[ task setLaunchPath: @"/usr/bin/lspci" ];
[ task setArguments:[NSArray arrayWithObject:@"-nn"]];
[ task setStandardOutput: pipe ];
[ task setStandardError: pipe ];
[ task launch ];
[ task waitUntilExit];
[ task release];
NSData *outputData = [[pipe fileHandleForReading] readDataToEndOfFile];
NSString *outputString = [[[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding] autorelease];
As /usr/bin/lspci
does not exist on some systems, this fatal error occurs
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'launch path not accessible'
How to check beforehand that lspci
does exist, and if it doesnt then display error message to user?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查文件是否存在并且可执行:
丢失文件并不是出现异常的唯一原因。您应该始终使用
@try
-@catch
块。To check if file exists and is executable:
Missing file is not the only reason why you can get an exception. You should always use
@try
-@catch
block.