Linux でも Swift が使えるようになったので、まずは Docker を使って Linux Mint 17.2 に導入してみたのですが(参照)、Docker なんぞを使うと却って面倒なので、お手軽にやってみました。上の公式サイトに書いてあるとおりにやっただけです。正式に暗号鍵を使ってダウンロードなど面倒なので、デジタル署名は使っていません(これはいわば「お手軽」版なので、きちんとやりたい方は暗号鍵を使った方法でやって下さい)。
まず、clang 関係で必要なものを入れます。
$ sudo apt-get install clang libicu-dev
次に、公式サイトから自分の環境に合った Snapshot をダウンロードして下さい。自分はふつうに Swift 3.0 を選びました(Ubuntu 14.04)。
適当に解凍して好きな場所に置きます。僕は ~/Documents/Swift/ 以下に usr フォルダを置きました。
もうこれで使えます。インストールなど不要なので、とてもお手軽です。僕の環境なら
$ ~/Documents/Swift/usr/bin/swift
で REPL(対話型実行環境)が立ち上がりますが、これでは面倒なので
$ export PATH=$HOME/Documents/Swift/usr/bin:$PATH
とかします(各自の環境に合わせて下さい)。これで単に
$ swift
で REPL を起動できます。
バージョンの確認は
$ swift --version Swift version 3.0 (swift-3.0-RELEASE) Target: x86_64-unknown-linux-gnu
となりました。
あとは過去記事と同じです。よろしければ参考にして下さい。
ちなみに、REPL から抜けるやり方がわからず、しばらく悩みました(笑)。結論を書くと、単に :exit です。:help でヘルプということからわかりました。はは。
REPL 実行例
$ swift Welcome to Swift version 3.0 (swift-3.0-RELEASE). Type :help for assistance. 1> let a = "Hello!" a: String = "Hello!" 2> print(a) Hello! 3> a. Available completions: append(c: Character) -> Void append(contentsOf: S) -> Void append(other: String) -> Void characters: String.CharacterView customMirror: Mirror customPlaygroundQuickLook: PlaygroundQuickLook debugDescription: String description: String distance(from: String.Index, to: String.Index) -> String.IndexDistance endIndex: String.Index hashValue: Int index(after: String.Index) -> String.Index index(before: String.Index) -> String.Index index(i: String.Index, offsetBy: String.IndexDistance) -> String.Index index(i: String.Index, offsetBy: String.IndexDistance, limitedBy: String.Index) -> String.Index? insert(contentsOf: S, at: String.Index) -> Void insert(newElement: Character, at: String.Index) -> Void isEmpty: Bool lowercased() -> String remove(at: String.Index) -> Character removeAll() -> Void removeAll(keepingCapacity: Bool) -> Void removeSubrange(bounds: ClosedRange<String.Index>) -> Void removeSubrange(bounds: Range<String.Index>) -> Void replaceSubrange(bounds: ClosedRange<String.Index>, with: C) -> Void replaceSubrange(bounds: ClosedRange<String.Index>, with: String) -> Void replaceSubrange(bounds: Range<String.Index>, with: C) -> Void replaceSubrange(bounds: Range<String.Index>, with: String) -> Void reserveCapacity(n: Int) -> Void startIndex: String.Index unicodeScalars: String.UnicodeScalarView uppercased() -> String utf16: String.UTF16View utf8: String.UTF8View utf8CString: ContiguousArray<CChar> withCString(body: (UnsafePointer<Int8>) throws -> Result(UnsafePointer<Int8>) throws -> Result) -> Result withMutableCharacters(body: (inout String.CharacterView) -> R(inout String.CharacterView) -> R) -> R write(other: String) -> Void write(to: &Target) -> Void 3> a.hasValue error: repl.swift:3:1: error: value of type 'String' has no member 'hasValue' a.hasValue ^ ~~~~~~~~ 3> a.lowercased $R0: () -> String = 0x00007ffff7f641c0 $__lldb_expr7`rt_swift_allocObject + 16 4> a.lowercased() $R1: String = "hello!" 5> :exit
注記しておくと、a. の下に長いリストが出てきているのは、じつは a. のあとに [Tab] を入力していて、使えるメソッドが列挙されています。a.re として [Tab] を押せば、re で始まるメソッドが列挙されます。
また、上下のカーソルキーも上手く使って下さい。