Swift是apple 提供的一個開法在ISO跟OS X上AP的工具 根據官網描述 是一個具有高開發跟效能的語言
Swift是base on cocoa跟cocoa touch框架
網路上有好心人放上翻譯的文件swift 中文翻譯檔
2017 apple 將Swift open出來 在pad上面有AP可以學習 並且有提供linux上面可以跑的bin file

安裝在 Linux

First, install clang:
$ sudo apt-get install clang
apple Swift file Apple 在網路上提供Ubuntu 14.04跟 16.04 16.10版本可執行檔

1
2
3
4
5
tar -jxvf swift-3.1.1-RELEASE-ubuntu14.04.tar.gz
export PATH=[youre folder path]/swift-3.1.1-RELEASE-ubuntu14.04/usr/bin:${PATH}
$ swift -version
> Swift version 3.1.1 (swift-3.1.1-RELEASE)
> Target: x86_64-unknown-linux-gnu

基本上解開檔案增加bin file path 到PATH下就可以使用了
目前最新的版本是3.1.1

Run Swift

Swift 有提供read-eval-print Loop(REPL)開發環境 當你純粹打swift後面沒有接任何參數時會進去

你可執行基本的運算或編程跟python一樣 例如

另外可以透過鍵查到相關function name, command 如下
"hi!".re<tab>

離開REPL command :exit :q

Build an executable

Create command

1
2
3
mkdir Test
cd Test
swift package init --type executable

build package

swift build
產生的執行檔在.build\debug下面

Debug support

Swift 可以透過LLDB debug

LLDB is the default debugger in Xcode on Mac OS X and supports debugging C, Objective-C and C++ on the desktop and iOS devices and simulator.

透過swiftc -g ***.swift 產生debug要用的檔案 再透過lldb *** 去執行debug

1
2
3
4
5
6
7
8
9
10
11
12
13
luna@luna-ubuntu14:Test$ cat Sources/main.swift
print("Hello, world!")
func factor(n: Int) -> Int{
if n <= 1 {return n}
return n * factor(n: n-1)
}
let number = 5
print("\(number)! is equal to \(factor(n: number))")
luna@luna-ubuntu14:Test$ swiftc -g Sources/main.swift
luna@luna-ubuntu14:Test$ ls
main Package.swift Sources Tests
luna@luna-ubuntu14:Test$ ls Sources/
main.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
luna@luna-ubuntu14:Test$ lldb main
(lldb) target create "main"
Current executable set to 'main' (x86_64).
(lldb) l
1 print("Hello, world!")
2 func factor(n: Int) -> Int{
3 if n <= 1 {return n}
4 return n * factor(n: n-1)
5 }
6
7 let number = 5
8 print("\(number)! is equal to \(factor(n: number))")
(lldb) b 2
Breakpoint 1: where = main`main.factor (n : Swift.Int) -> Swift.Int + 12 at main.swift:3, address = 0x0000000000400f5c
(lldb) r
Process 8098 launched: '/home/luna/swift/Test/main' (x86_64)
Hello, world!
Process 8098 stopped
* thread #1, name = 'main', stop reason = breakpoint 1.1
frame #0: 0x0000000000400f5c main`factor(n=5) -> Int at main.swift:3
1 print("Hello, world!")
2 func factor(n: Int) -> Int{
-> 3 if n <= 1 {return n}
4 return n * factor(n: n-1)
5 }
6
7 let number = 5
(lldb) p n
(Int) $R0 = 5
(lldb) n
Process 8098 stopped
* thread #1, name = 'main', stop reason = step over
frame #0: 0x0000000000400f70 main`factor(n=5) -> Int at main.swift:4
1 print("Hello, world!")
2 func factor(n: Int) -> Int{
3 if n <= 1 {return n}
-> 4 return n * factor(n: n-1)
5 }
6
7 let number = 5
(lldb) n
* thread #1, name = 'main', stop reason = breakpoint 1.1
frame #0: 0x0000000000400f5c main`factor(n=4) -> Int at main.swift:3
1 print("Hello, world!")
2 func factor(n: Int) -> Int{
-> 3 if n <= 1 {return n}
4 return n * factor(n: n-1)
5 }
6
7 let number = 5
(lldb) p n
(Int) $R1 = 4
(lldb) p n*n
(Int) $R2 = 16

參考資料

Apple Swift