The Polyglot Web

Web Coding in Swift

Unfortunately, Swift is not installed on this server. Functions are disabled.

Swift is Apple's new language intended to make app development easier than using Objective-C. However, there are many features not yet available in Swift, so sometimes we have to make Objective-C function calls that use the Cocoa library to do I/O and other operations.

Swift is a compiled language, but modules can also be executed as scripts. I tried to run the code using this line, which I put at the top of each file:

#!/usr/bin/env xcrun swift

When running the module as scripts, it becomes difficult to link my own library code. So, instead, I first compile the common module like this:

xcrun swiftc \
    -emit-library \
    -module-name common \
    -emit-module common.swift \
    -sdk $(xcrun --show-sdk-path --sdk macosx)

Then I compile and link the remaining modules using this script, which I named maks (for Make Swift). This command tells Swift to look for the include files and resulting libraries in this current directory. It also saves the executable file with the cgi file extension.

#!/bin/sh
/usr/bin/env xcrun swiftc -o $1.cgi -I "." -L "." -lcommon \
  -module-link-name common \
  -sdk $(xcrun --show-sdk-path --sdk macosx) $1.swift