UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to run an external program using Process

Swift version: 5.6

Paul Hudson    @twostraws   

If you’re building an app for macOS or any other platform where you can run external programs, you can draw on Foundation’s Process class to do almost all the work for you.

First, create an instance of Process:

let task = Process()

Next, tell it which command to run. I’ll make it run the Swift compiler:

task.executableURL = URL(fileURLWithPath: "/usr/bin/swift")

Finally, pass in an array of arguments you want to give to the program. For example, if you have some Swift code you wanted to run you could pass the filename to that code:

let filename = "input.swift"
task.arguments = [filename]

When you’re ready, use the run() method to run the full command, being prepared to catch any errors that are thrown:

try task.run()

If you want to read the output or error from your program, you need to create an instance of Pipe and attach it either to standardOutput or standardError depending on your needs:

let outputPipe = Pipe()
let errorPipe = Pipe()

task.standardOutput = outputPipe
task.standardError = errorPipe

Make sure you do that before you call run().

To read the output or error data once your program completes, you need to get a file handle from the pipe then read it out as an instance of Data, like this:

let outputData = outputPipe.fileHandleForReading.readDataToEndOfFile()
let errorData = errorPipe.fileHandleForReading.readDataToEndOfFile()

Finally, convert that data into strings if you need to, like this:

let output = String(decoding: outputData, as: UTF8.self)        
let error = String(decoding: errorData, as: UTF8.self)

And that’s it – you’ve run a program with custom arguments, and read its output back.

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Available from iOS 8.0

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 3.9/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.