簡體   English   中英

Objective-C中的NSTask

[英]NSTask in Objective-C

因此,我試圖從程序中運行一些終端命令,但出現一些令人困惑的錯誤。

我是來自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
 )

謝謝!

您的arguments數組包含一個數組。 它應該是一個字符串數組。 將您的commands對象用作NSTask的參數。

我認為問題在於您將帶有字符串數組的數組傳遞給setArguments 您應該傳遞僅包含字符串而不是嵌套數組的數組。

但是我認為您會誤解NSTask的參數是NSTask工作的。 您可能應該執行以下操作:

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

等等,或者如果您真的想使用sh ,則可能需要添加一些; 分隔外殼命令。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM