Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective-C中的NSTask_Objective C_Sh_Nstask - Fatal编程技术网

Objective-C中的NSTask

Objective-C中的NSTask,objective-c,sh,nstask,Objective C,Sh,Nstask,所以我试着从我的程序中运行一些终端命令,我得到了一些令人困惑的错误 我是一个来自Java的新开发人员,所以我可能遗漏了一些东西 代码如下: NSTask *task = [[NSTask alloc] init]; NSString *commitText = [commitMessage stringValue]; NSString *a = [NSString stringWithFormat:@"cd %@", dirPath]; NSString *c = [NSString strin

所以我试着从我的程序中运行一些终端命令,我得到了一些令人困惑的错误

我是一个来自Java的新开发人员,所以我可能遗漏了一些东西

代码如下:

NSTask *task = [[NSTask alloc] init];
NSString *commitText = [commitMessage stringValue];
NSString *a = [NSString stringWithFormat:@"cd %@", dirPath];
NSString *c = [NSString stringWithFormat:@"git commit -m '%@'", commitText];
NSArray *commands = [[NSArray alloc]initWithObjects:a,
                     @"git add 'Project'",
                     c,
                     @"git push origin HEAD",
                     nil];
[task setLaunchPath:@"/bin/sh"];

// Do commands
NSArray *args = [NSArray arrayWithObjects:commands,
                 nil];

[task setArguments: args];
[task launch];
以下是错误:

2012-06-09 08:35:20.561 Auto Git[5433:403] -[__NSArrayI fileSystemRepresentation]:                 unrecognized selector sent to instance 0x7fb250d6a1e0
2012-06-09 08:35:20.561 Auto Git[5433:403] -[__NSArrayI fileSystemRepresentation]: unrecognized selector sent to instance 0x7fb250d6a1e0
2012-06-09 08:35:20.679 Auto Git[5433:403] (
  0   CoreFoundation                      0x00007fff870b4f56 __exceptionPreprocess + 198
  1   libobjc.A.dylib                     0x00007fff90e35d5e objc_exception_throw + 43
  2   CoreFoundation                      0x00007fff871411be -[NSObject doesNotRecognizeSelector:] + 190
  3   CoreFoundation                      0x00007fff870a1e23 ___forwarding___ + 371
  4   CoreFoundation                      0x00007fff870a1c38 _CF_forwarding_prep_0 + 232
  5   Foundation                          0x00007fff9174f3a3 -[NSConcreteTask launchWithDictionary:] + 901
  6   Auto Git                            0x000000010d83c6db -[Push push:] + 571
  7   CoreFoundation                      0x00007fff870a470d -[NSObject performSelector:withObject:] + 61
  8   AppKit                              0x00007fff8e0f8f7e -[NSApplication sendAction:to:from:] + 139
  9   AppKit                              0x00007fff8e0f8eb2 -[NSControl sendAction:to:] + 88
  10  AppKit                              0x00007fff8e0f8ddd -[NSCell _sendActionFrom:] + 137
  11  AppKit                              0x00007fff8e0f82a0 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2014
  12  AppKit                              0x00007fff8e177fc4 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 489
  13  AppKit                              0x00007fff8e0f6eaa -[NSControl mouseDown:] + 786
  14  AppKit                              0x00007fff8e0c2348 -[NSWindow sendEvent:] + 6306
  15  AppKit                              0x00007fff8e05ba55 -[NSApplication sendEvent:] + 5593
  16  AppKit                              0x00007fff8dff20c6 -[NSApplication run] + 555
  17  AppKit                              0x00007fff8e26e244 NSApplicationMain + 867
  18  Auto Git                            0x000000010d83bff2 main + 34
  19  Auto Git                            0x000000010d83bfc4 start + 52
  20  ???                                 0x0000000000000003 0x0 + 3
 )

谢谢

我认为问题在于您将带有字符串数组的数组传递给
setArguments
。您应该只传递带有字符串的数组,而不是嵌套数组

但是我认为您误解了
NSTask
的参数是如何工作的。您可能应该这样做:

[task setArguments:[[[NSArray alloc] initWithObjects:
                     @"git", @"add", @"Project", nil]
                    autorelease]];

等等,或者如果你真的想使用
sh
,你可能需要添加一些
来分隔shell命令。

参数数组包含一个数组。它应该是一个字符串数组。使用
命令
对象作为NSTask的参数。

谢谢。我对“cd%@”这个dirPath也有问题,因为它说没有这样的dir示例/bin/sh:cd/Users/webstreet/Documents/GIT/findmycar:没有这样的文件或目录你不应该也使用
-c
选项来
sh
吗?我不太清楚我是新手。我想你应该在这本书中读一下
-c
。如果您想测试它的行为,请尝试使用交互式shell。提示:
sh-c回声测试
不起作用,但
sh-c“回声测试”
会起作用。但我认为在您的情况下,一个更健壮的解决方案是将每个命令作为单独的
NSTask
launch
:s执行。另一种解决方案是在应用程序中包含一个helper shell脚本,该脚本将
dirPath
commitText
作为参数并执行命令,然后启动一个
NSTask
来运行它。