【问题标题】:NSTask not working in SwiftNSTask 在 Swift 中不起作用
【发布时间】:2016-02-18 00:11:09
【问题描述】:

我正在编写一个快速 XCTest,我需要评估一个子流程。我想调用终端,将applescript的路径传递给终端并执行它。

我已经在 swift 测试文件中导入了 UIKit 和 Foundation。当我去写一个常量时:let task = NSTask() NSClass 没有在 NSObject 中引用,结果我收到一条消息,它是一个

使用未解析的非标识符 NSTask

如果我写了let pipe = NSPipe(),那么它就会被引用并且可以工作。为什么导入 UIkit 和 Foundation 后 NSTask 无法访问?

【问题讨论】:

  • NSTask 在 iOS 上不可用
  • 那么这就解释了。谢谢@user3441734

标签: swift2 xcode7 xctest


【解决方案1】:

在 swift 3.0 Apple 更改了 NSTask API

现在执行一个cmd是:

// Create a process (was NSTask on swift pre 3.0)
let task = Process()

// Set the task parameters
task.launchPath = "/bin/ls"
task.arguments = ["-laF@" , filePath]

// Create a Pipe and make the task
// put all the output there
let pipe = Pipe()
task.standardOutput = pipe

// Launch the task
task.launch()

// Get the data
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: String.Encoding.utf8)
print(output!)

【讨论】:

  • Process() 仍然说:在 swift 3 Xcode 8.1(导入了基础)中“使用未解析的标识符进程”有什么建议吗?
  • 现在在 Xcode 版本 8.0 (8A218a) 下测试:1) OSX 应用程序:可以工作 2) 可可终端应用程序:可以工作。
  • 确实如此,我是在 iOS 中开发的,所以它不起作用。我的错!
  • 为什么 Apple 允许它在 OSX 而不是 iOS 中工作?我们在移动端也需要在tests/other中访问终端命令。
  • 不,这是一种设计选择:iOS 中没有通常的“进程”,原因有很多:性能、完善、硬件限制等等。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-05
  • 2014-07-27
相关资源
最近更新 更多