-
Book Overview & Buying
-
Table Of Contents
Swift Cookbook
By :
Like some other script languages, you can use Swift with its interpreter on the command line. Sometimes it's very useful, mainly when you want to test code but you don't want to create a new playground.
Open a terminal window as it was shown in the previous recipe.
Follow these steps for using Swift as a command line interpreter:
xcode-select command, it is possible that the Swift command is not accessible from your PATH variable. So, you can localize your Swift command using find /Applications/Xcode.app -name swift -a -type f. In my case, I got /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift. However, current versions of Xcode have the Swift command at /usr/bin. If it is necessary, add this directory to your PATH variable with the command export PATH="$PATH:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/". At this moment, we can enter into the Swift interpreter just typing swift.PATH variable set permanently. To make this, we need to add the previous command into our .profile file, such as echo 'export PATH="$PATH:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/" ' >> $HOME/.profile && chmod +x $HOME/.profile. From now on, if you restart your computer, it won't be necessary to look for the Swift path and set the PATH environment variable again.var dividend = [3,2,1,0] var divisor = 6
for i in dividend {
println("\(divisor) / \(i) = \(divisor / i)")
}6 / 3 = 2 6 / 2 = 3 6 / 1 = 6 Execution interrupted. Enter Swift code to recover and continue. Enter LLDB commands to investigate (type :help for assistance.)
Calling the Swift command gives you the possibility to test your code or even use Swift as a scripting language. The highlight here is that you need to know where your Swift command is; the command line helps you to find it.
Most of Swift's options and swiftc options are common; it means that if there is something that you would like to test before compiling, you can do it.
Change the font size
Change margin width
Change background colour