Hello, World in Swift

Posted on Sun 03 June 2018 in Swift

Of course, we start with printing. Here is "Hello, World!" in Swift. It couldn't be much simpler.

// hello.swift  [Swift 4.1; Xcode 9.3.1; 2018-05-27]
//
// Just say "Hello".
//

print("Hello, World!")

The //, of course, introduce comments, print is the print function, and "Hello, World!" is the string to be printed. We don't need a semicolon (or anything else) to terminate the line.

We can run this various ways, but the main one is to compile it and then run it. Assuming you have Xcode installed (you might also need Xcode's command-line tools; I'm not sure), you can compile the code with

$ swiftc hello.swift

(assuming you've stored this in hello.swift) which produces the executable file swift. And running that:

$ ./hello
Hello, World!

as expected, prints Hello, World! to stdout.