Cocoa 运行命令行工具时的NSTask和参数
在这段代码中,如何将参数(本例中为主机)传递给NSTask?它不接受主机Cocoa 运行命令行工具时的NSTask和参数,cocoa,nstask,Cocoa,Nstask,在这段代码中,如何将参数(本例中为主机)传递给NSTask?它不接受主机NSString。如果我通过ping传递主机值,例如 [NSArray arrayWithObjects:@"-c",@"ping -c 5 www.google.com",nil] 然后它就起作用了。但它不会单独使用主机参数。提前谢谢你的帮助 task = [[NSTask alloc] init]; [pipe release]; pipe = [[NSPipe alloc] init]; [task setStan
NSString
。如果我通过ping传递主机值,例如
[NSArray arrayWithObjects:@"-c",@"ping -c 5 www.google.com",nil]
然后它就起作用了。但它不会单独使用主机参数。提前谢谢你的帮助
task = [[NSTask alloc] init];
[pipe release];
pipe = [[NSPipe alloc] init];
[task setStandardInput: [NSPipe pipe]];
[task setLaunchPath:@"/bin/bash"];
NSArray *args = [NSArray arrayWithObjects:@"-c",@"ping -c 5",host,nil];
[task setArguments:args];
[task setStandardOutput:pipe];
NSFileHandle *fh = [pipe fileHandleForReading];
你的论点不正确。首先,应该将launchpath设置为/bin/ping,或者无论任务位于何处,然后参数应该是通常在命令行中输入的参数的数组,然后用空格分隔
有关如何正确执行此操作的详细信息,请参阅本教程。使用
NSString
类的stringWithFormat
方法
task = [[NSTask alloc] init];
[pipe release];
pipe = [[NSPipe alloc] init];
[task setStandardInput: [NSPipe pipe]];
[task setLaunchPath:@"path"];
NSArray *args = [NSArray arrayWithObjects:@"-c",[NSString stringWithFormat: @"%@ %@ %@ %@",@"ping",@"-c",@"5",host],nil];
[task setArguments:args];
[task setStandardOutput:pipe];
NSFileHandle *fh = [pipe fileHandleForReading];
Bash-c需要用引号引用您的命令
NSMutableArray *args = [NSMutableArray array];
NSArray *args = [NSArray arrayWithObjects:@"-c", @"\"ping -c 5", host, @"\"",nil]
[task setArguments:args];