Wednesday, August 6, 2014

Run Swift in Terminal and Script

You may have heard that the Apple's new programming language Swift can work interactively, which means when you type a line of code, you could see the result in Terminal. It's very useful when developer want to try some line of code of Swift and not for sure it is correct. Or it is a perfect way for new comer to learn the new language from beginning. How to do that? Please follow instructions here.

First, run the following command to set the path of a developer directory:

$ sudo xcode-select -switch /Applications/Xcode6-Beta5.app/Contents/Developer/

This step doesn't started a session of Swift shell, but this step is must-to-have. You may use another version of Xcode, so the developer's directory maybe different. Please check if the directory you switch to exists. It is usually with the format like /Applications/<Xcode-Version>.app/Contents/Developer/.

Remember you have to add sudo prior to the xcode-select command, otherwise you will get the error message like this:

xcode-select: error: --switch must be run as root (e.g. sudo xcode-select --switch <xcode_folder_path>).

Second, run xcrun swift to start a Swift interactive session(we don't require adding sudo anymore this time), here we run a "Hello World!" sample:

$ xcrun swift
Welcome to Swift!  Type :help for assistance.
  1> println("Hello, world!")
Hello, world!

And then type the following code to try a function which is from Apple's Swift Tour:

  2> func sumOf(numbers: Int...) -> Int { 
  3.     var sum = 0 
  4.     for number in numbers { 
  5.         sum += number 
  6.     } 
  7.     return sum 
  8. }    
  9> sumOf()
$R0: Int = 0
 10> sumOf(42, 597, 12)
$R1: Int = 651

You can see that the return values of the function is printed as the format $R<num>: <RetType> = <RetValue> with different color. That's much more clear and user friendly.

OK, that's what I show you the way to play around Swift session. An alternative way you can start the session is using lldb --repl command. The session started by the command looks the same.

You may want to see the history of the session, you can use arrow keys up and down to check previous or next Swift code line you typed. The history of Swift session of each user is kept in the file located at ~/.lldb/lldb-swift-history.

Hmm... It looks like other scripting language like Unix Shell and Python. Similar to these scripting language, Swift also works in script.

For example, create a file named swift_test like this:

#!/usr/bin/env xcrun swift
println("Hello World!")

class Shape {
    var numberOfSides = 0
    func simpleDescription() -> String {
        return "A shape with \(numberOfSides) sides."
    }
}

var shape = Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()

println("Shape's simple description : " + shapeDescription)

Here we define a Swift class to see if it works, everything goes well it we run the script in command line:

$ ./swift_test 
Hello World!
Shape's simple description : A shape with 7 sides.

Very powerful, isn't it? It looks Swift not only works for developing App. With the library support, it could also be used for system management and automation.

No comments:

Post a Comment