Laser Hawk
Laser Hawk

Reputation: 2028

NSTask not working in Swift

I am writing a swift XCTest and I need to assess a sub process. I want to call the terminal, pass the path of an applescript to the terminal and execute it.

I've imported both UIKit and Foundation in the swift test file. When I go to write a constant like: let task = NSTask() the NSClass is not referenced in the NSObject, and as a result I get a message that it is a

Use of unresolved unidentifier NSTask

If I write let pipe = NSPipe() that is referenced and works. Why is NSTask inaccessible after importing UIkit and Foundation?

Upvotes: 3

Views: 1967

Answers (1)

ingconti
ingconti

Reputation: 11646

in swift 3.0 Apple changed NSTask API

To execute a cmd now is:

// 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!)

Upvotes: 3

Related Questions