不一樣的開始
好吧乏屯,你打開這篇文章說明你對Swift有一定了解疫铜,我就不廢話告訴你這是什么玩意了。
盡管Swift的“用武之地”主要是開發(fā)IOS或者macOS應用辙浑,但是它在Web開發(fā)方面也不遜色于傳統(tǒng)的Web端開發(fā)語言,盡管性能與Golang等相比有一定差距衷蜓,但是Swift的優(yōu)勢在于你可以學習一門語言就能完成從桌面端累提、移動端、服務器端的全部后端開發(fā)工作恍箭。當然桌面端和移動端僅限蘋果產(chǎn)品刻恭。也就是說如果你的產(chǎn)品集中在蘋果平臺瞧省,Swift大概是個不二的選擇扯夭。
除此之外讓人想用Swift來開發(fā)Web端的另一個原因是Swift的語法讓人感覺舒服。就開發(fā)效率來說鞍匾,個人認為是可以媲美Python交洗、Ruby的。
蘋果平臺安裝Swift我就不廢話了橡淑,Windows和Linux的話也是解壓就能用构拳,沒什么好說的。
因為是教程梁棠,所以使用Docker安裝不同版本的Swift置森,以便更好地控制版本,比較版本之間的差異符糊。
運行 Swift 的 Docker 鏡像
首先凫海,我們需要Swift開發(fā)環(huán)境,本文使用Docker構(gòu)建一個用于Swift簡單開發(fā)的在線IDE(基于Cloud9 IDE)男娄。
安裝Docker不廢話行贪,一句話:
curl -sSL https://get.docker.com/ | sh
安裝好Docker之后使用下面這句話就可以啟動一個用于Swift開發(fā)的容器了。自己改用戶名密碼模闲。
docker run -d -it --privileged=true --name=cloud9 -v ~/workspace:/root/workspace -p 8181:8181 zuolan/swift-ide --auth username:password
如果你想自己構(gòu)建一個鏡像建瘫,這份 Dockerfile 可以在Github找到。
Dockerfile
FROM ubuntu:trusty
MAINTAINER ZuoLan <i@zuolan.me>
# If you use Swift package, may be need install libicu-dev, so you can use libicu-dev instead of libicu52.
ENV buildDeps="make build-essential g++ gcc curl ca-certificates git" c9Deps="nodejs" swiftDeps="curl python-dev libedit2 clang libicu52 libxml2"
# Install Cloud9-ide
RUN apt-get update && apt-get upgrade -y \
&& apt-get install -y $buildDeps --no-install-recommends \
&& curl -sL https://deb.nodesource.com/setup_4.x | sudo bash - \
&& apt-get -y install $c9Deps $swiftDeps \
&& npm install -g forever && npm cache clean \
&& git clone https://github.com/c9/core.git /cloud9 && cd /cloud9 \
&& scripts/install-sdk.sh \
# Install Swift
&& cd /usr/local/ \
&& curl -o swift.tar.gz -sL https://swift.org/builds/swift-3.0-release/ubuntu1404/swift-3.0-RELEASE/swift-3.0-RELEASE-ubuntu14.04.tar.gz \
&& tar xzf swift.tar.gz && mv swift-3.0-RELEASE-ubuntu14.04 swift && rm /usr/local/swift.tar.gz \
&& echo 'export PATH=/usr/local/swift/usr/bin:"${PATH}"' >> ~/.bashrc \
&& apt-get autoremove -y $buildDeps \
&& apt-get autoremove -y && apt-get autoclean -y && apt-get clean -y \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& npm cache clean
VOLUME /root/workspace
ENV workspace /root/workspace
EXPOSE 8181
ENTRYPOINT ["forever", "/cloud9/server.js", "-w", "/root/workspace", "-l", "0.0.0.0"]
#CMD["--auth","username:password"]
使用包管理器
雖然是新手尸折,但是基本的概念還是沒問題的對吧啰脚?
$ mkdir swift # 新建一個文件夾
$ cd swift
$ swift package init # 初始化項目
$ tree # 看一下項目目錄結(jié)構(gòu)
.
|-- Package.swift
|-- Sources
| `-- swift.swift
`-- Tests
|-- LinuxMain.swift
`-- swiftTests
`-- swiftTests.swift
3 directories, 4 files
$ swift build # 構(gòu)建項目
$ swift test # 測試項目
構(gòu)建可執(zhí)行文件
再新建一個項目吧!~
$ mkdir swift-ex # 新建一個文件夾
$ cd swift-ex
$ swift package init --type executable # 初始化項目
$ swift build
Creating executable package: swift-ex
Creating Package.swift
Creating .gitignore
Creating Sources/
Creating Sources/main.swift
Creating Tests/
$ swift build
Compile Swift Module 'swift_ex' (1 sources)
Linking ./.build/debug/swift-ex
$ ./.build/debug/swift-ex # 執(zhí)行可執(zhí)行文件
Hello, world!
這樣第一個Hello World項目就出來了实夹。
使用多個源文件
再來一個多文件的項目拣播,算熟悉一下這些工具吧,反正還沒正式開始進入學習呢收擦。
編輯一個文件:$ vi Greeter.swift
這里使用vi編輯器是因為在Linux環(huán)境下啊贮配,教程基礎(chǔ)部分都是在Linux下操作的,一個編輯器而已塞赂,不用在意泪勒。
文件內(nèi)容:
func sayHello(name: String) {
print("Hello, \(name)!")
}
再編輯一個文件:$ vi main.swift
,內(nèi)容如下:
if CommandLine.arguments.count != 2 {
print("Usage: hello NAME")
} else {
let name = CommandLine.arguments[1]
sayHello(name: name)
}
www(笑),現(xiàn)在就可以構(gòu)建了圆存,然后運行一下叼旋,第二個Hello World項目達成。
$ swift build
Compile Swift Module 'swift_ex' (2 sources)
Linking /root/workspace/swift-ex/.build/debug/swift-ex
$ /root/workspace/swift-ex/.build/debug/swift-ex zuolan
Hello, zuolan!
使用LLDB調(diào)試器
調(diào)試這個看看就好沦辙,以后還會遇到夫植。
編輯文件:$ vi Factorial.swift
func factorial(n: Int) -> Int {
if n <= 1 { return n }
return n * factorial(n: n - 1)
}
let number = 4
print("\(number)! is equal to \(factorial(n: number))")
編譯一下:
$ swiftc -g Factorial.swift
看一下文件目錄結(jié)構(gòu):
~/workspace/swift-ex/Sources $ tree
.
|-- Factorial
|-- Factorial.swift
|-- Greeter.swift
`-- main.swift
0 directories, 4 files
關(guān)于更多LLDB的命令可以在 LLDB Tutorial 中查閱。
進入調(diào)試模式:
$ lldb Factorial
(lldb) target create "Factorial"
Current executable set to 'Factorial' (x86_64).
(lldb) b 2
Breakpoint 1: where = Factorial`Factorial.factorial (n : Swift.Int) -> Swift.Int + 12 at Factorial.swift:2, address = 0x00000000004010ac
(lldb) r
Process 1834 launched: '/root/workspace/swift-ex/Sources/Factorial' (x86_64)
Process 1834 stopped
* thread #1: tid = 1834, 0x00000000004010ac Factorial`factorial(n=4) -> Int + 12 at Factorial.swift:2, name = 'Factorial', stop reason = breakpoint 1.1
frame #0: 0x00000000004010ac Factorial`factorial(n=4) -> Int + 12 at Factorial.swift:2
1 func factorial(n: Int) -> Int {
-> 2 if n <= 1 { return n }
3 return n * factorial(n: n - 1)
4 }
5
6 let number = 4
7 print("\(number)! is equal to \(factorial(n: number))")
(lldb) p n
(Int) $R0 = 4
(lldb) p n * n
(Int) $R1 = 16
(lldb) bt
* thread #1: tid = 1834, 0x00000000004010ac Factorial`factorial(n=4) -> Int + 12 at Factorial.swift:2, name = 'Factorial', stop reason = breakpoint 1.1
* frame #0: 0x00000000004010ac Factorial`factorial(n=4) -> Int + 12 at Factorial.swift:2
frame #1: 0x0000000000400fda Factorial`main + 282 at Factorial.swift:7
frame #2: 0x00007ffff6e6bf45 libc.so.6`__libc_start_main + 245
frame #3: 0x0000000000400df9 Factorial`_start + 41
(lldb) c
Process 1834 resuming
Process 1834 stopped
* thread #1: tid = 1834, 0x00000000004010ac Factorial`factorial(n=3) -> Int + 12 at Factorial.swift:2, name = 'Factorial', stop reason = breakpoint 1.1
frame #0: 0x00000000004010ac Factorial`factorial(n=3) -> Int + 12 at Factorial.swift:2
1 func factorial(n: Int) -> Int {
-> 2 if n <= 1 { return n }
3 return n * factorial(n: n - 1)
4 }
5
6 let number = 4
7 print("\(number)! is equal to \(factorial(n: number))")
(lldb) p n
(Int) $R2 = 3
(lldb) br di
All breakpoints disabled. (1 breakpoints)
(lldb) c
Process 1834 resuming
4! is equal to 24
Process 1834 exited with status = 0 (0x00000000)
(lldb) ^D
~/workspace/swift-ex/Sources $
最后一部分沒什么好看的油讯,剛開始基本用不上详民。