Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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
Android Swift can';t执行adb和命令行指令_Android_Swift_Macos_Shell - Fatal编程技术网

Android Swift can';t执行adb和命令行指令

Android Swift can';t执行adb和命令行指令,android,swift,macos,shell,Android,Swift,Macos,Shell,我正在尝试在macOS应用程序中使用swift执行命令行指令。 代码如下: @discardableResult static func shell(_ args: String...) -> Int32 { let task = Process() task.launchPath = "/usr/bin/env" task.arguments = args task.launch() task.waitUntilExit() return

我正在尝试在macOS应用程序中使用swift执行命令行指令。 代码如下:

@discardableResult
static func shell(_ args: String...) -> Int32
{
    let task = Process()
    task.launchPath = "/usr/bin/env"
    task.arguments = args
    task.launch()
    task.waitUntilExit()
    return task.terminationStatus
}

@discardableResult
static func shellAdb(_ args: String...) -> Int32
{
    let task = Process()
    task.launchPath = "/Users/myname/Library/Android/sdk/platform-tools"
    task.arguments = args
    task.launch()
    task.waitUntilExit()
    return task.terminationStatus
}
我使用'adb--v'和'echo hello'字符串并将其传递给'shell'函数

我还尝试了第一个命令的“shellAdb”函数,但它也不起作用

如果使用“shell”函数,则会出现以下错误:

env: adb --v: No such file or directory
env: echo 'hello': No such file or directory
两者的错误代码均为“127”

如果使用“shellAdb”函数,则会出现以下其他错误:

[General] Couldn't posix_spawn: error 13
[General] (
0   CoreFoundation                      0x00007fffc5ab52cb __exceptionPreprocess + 171
1   libobjc.A.dylib                     0x00007fffda8c048d objc_exception_throw + 48
2   CoreFoundation                      0x00007fffc5b33c3d +[NSException raise:format:] + 205
3   Foundation                          0x00007fffc74de54e -[NSConcreteTask launchWithDictionary:] + 3134
4   AndroidPaste                        0x0000000100003684 _TZFC12AndroidPaste17TerminalCommandEx8shellAdbftGSaSS__Vs5Int32 + 340
5   AndroidPaste                        0x0000000100002520 _TFC12AndroidPaste14ViewController11pasteActionfCSo8NSButtonT_ + 640
6   AndroidPaste                        0x000000010000269a _TToFC12AndroidPaste14ViewController11pasteActionfCSo8NSButtonT_ + 58
7   libsystem_trace.dylib               0x00007fffdb3d73a7 _os_activity_initiate_impl + 53
8   AppKit                              0x00007fffc3ca3721 -[NSApplication(NSResponder) sendAction:to:from:] + 456
9   AppKit                              0x00007fffc3787cc4 -[NSControl sendAction:to:] + 86
10  AppKit                              0x00007fffc3787bec __26-[NSCell _sendActionFrom:]_block_invoke + 136
11  libsystem_trace.dylib               0x00007fffdb3d73a7 _os_activity_initiate_impl + 53
12  AppKit                              0x00007fffc3787b44 -[NSCell _sendActionFrom:] + 128
13  AppKit                              0x00007fffc37ca539 -[NSButtonCell _sendActionFrom:] + 98
14  libsystem_trace.dylib               0x00007fffdb3d73a7 _os_activity_initiate_impl + 53
15  AppKit                              0x00007fffc3786426 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2481
16  AppKit                              0x00007fffc37ca272 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 798
17  AppKit                              0x00007fffc3784ddb -[NSControl mouseDown:] + 832
18  AppKit                              0x00007fffc3e1f24f -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 6341
19  AppKit                              0x00007fffc3e1ba6c -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 1942
20  AppKit                              0x00007fffc3e1af0a -[NSWindow(NSEventRouting) sendEvent:] + 541
21  AppKit                              0x00007fffc3c9f681 -[NSApplication(NSEvent) sendEvent:] + 1145
22  AppKit                              0x00007fffc351a427 -[NSApplication run] + 1002
23  AppKit                              0x00007fffc34e4e0e NSApplicationMain + 1237
24  AndroidPaste                        0x000000010000319d main + 13
25  libdyld.dylib                       0x00007fffdb1a5235 start + 1
这个问题在shell和shellAdb函数中都存在,但它们看起来像我已经找到的示例


我使用的是Swift 3、macOS 10.12.5和Xcode 8.3.2

shellAdb函数中的启动路径看起来像一个目录,而不是指向可执行文件的(完整)路径,应该是这样。@MartinR使用“shellAdb”函数解决了这个问题。我想知道为什么“echo hello”函数返回
127
错误。