Swift+Ubuntu environment configuration
First of all, it is assumed that we have installed the Ubuntu Linux operating system. The installation of this system is very simple. There are many step-by-step tutorials on the Internet. VirtualBox is recommended for virtual machines. Swift supports two versions of Ubuntu 14.04 and 15.10, I choose the 15.10 version of the package.
Step 1: Download the Swift 2.2
toolchain compressed package, open the terminal, enter the command to create a new directory and download it (you can go to swift.org to search)
diveinedu@diveinedu-VirtualBox:~$ mkdir swift && cd swift;
diveinedu@diveinedu-VirtualBox:~/swift$ wget https://swift.org/builds/swift-2.2-release/ubuntu1510/swift-2.2-RELEASE/swift-2.2-RELEASE-ubuntu15.10.tar.gz
Step 2: Use the tar
command to decompress the Swift 2.2
toolchain compressed package to the current directory, and configure environment variables
Unzip first, then enter the directory, there will be subdirectories usr/bin
and usr/lib
so on under the directory:
diveinedu@diveinedu-VirtualBox:~/swift$ tar xvf swift-2.2-RELEASE-ubuntu15.10.tar.gz
diveinedu@diveinedu-VirtualBox:~/swift$ cd swift-2.2-RELEASE-ubuntu15.10/
Then configure user-level environment variables and edit the $HOME/.bashrc
configuration file
diveinedu@diveinedu-VirtualBox:~/swift/swift-2.2-RELEASE-ubuntu15.10$ gedit $HOME/.bashrc
The above command will bring up the graphical interface text editor GEdit to edit this configuration file, enter the following configuration line at the end of the file and save and exit the editor
export SWIFT_HOME=$HOME/swift/swift-2.2-RELEASE-ubuntu15.10
export PATH=$SWIFT_HOME/usr/bin:$PATH
export LD_LIBRARY_PATH=$SWIFT_HOME/usr/lib:$LD_LIBRARY_PATH
export LIBRARY_PATH=$SWIFT_HOME/usr/lib:$LIBRARY_PATH
So the environment variables are configured OK. At this time, we only need to close our Shell terminal and reopen the terminal to make it effective.
3. Swift+Ubuntu first experience
Anyone who has been involved in iOS development knows that when Swift was first born in June 2014, Xcode brought the Playground function. You can read the results while writing. Is there anything similar under Ubuntu Linux? There are, but it’s not that powerful. Supported by the IDE, we can also run a Swift parser similar to the Pyhton script parser, and input the Swift
code synchronously to "parse" and run. This command swift
can be used directly after reopening the terminal after setting the above environment variables, as shown below.
diveinedu@diveinedu-VirtualBox:~$ swift
Welcome to Swift version 2.2-dev (LLVM 46be9ff861, Clang 4deb154edc, Swift 778f82939c). Type :help for assistance.
1> let hello = "hello";
hello: String = "hello"
2> let world = "diveinedu"
world: String = "diveinedu"
3> let space = " "
space: String = " "
4> print(hello+space+world);
hello diveinedu
5>hello.
Available completions:
append(c: Character) -> Void
append(x: UnicodeScalar) -> Void
appendContentsOf(newElements: S) -> Void
appendContentsOf(other: String) -> Void
characters: String.CharacterView
debugDescription: String
endIndex: Index
hashValue: Int
insert(newElement: Character, atIndex: Index) -> Void
insertContentsOf(newElements: S, at: Index) -> Void
isEmpty: Bool
lowercaseString: String
nulTerminatedUTF8: ContiguousArray<CodeUnit>
removeAll() -> Void
removeAll(keepCapacity: Bool) -> Void
removeAtIndex(i: Index) -> Character
removeRange(subRange: Range<Index>) -> Void
replaceRange(subRange: Range<Index>, with: C) -> Void
replaceRange(subRange: Range<Index>, with: String) -> Void
reserveCapacity(n: Int) -> Void
startIndex: Index
unicodeScalars: String.UnicodeScalarView
uppercaseString: String
utf16: String.UTF16View
utf8: String.UTF8View
withCString(f: UnsafePointer<Int8> throws -> ResultUnsafePointer<Int8> throws -> Result) -> Result
withMutableCharacters(body: (inout String.CharacterView) -> R(inout String.CharacterView) -> R) -> R
write(other: String) -> Void
writeTo(&target: Target) -> Void
6> hello.isEmpty
$R0: Bool = false
在这个解析执行界面还有自动提示补全功能!简直四国矣
.上面第五行是输入hello
后再输入一点.
然后按tab
键,一下就出来这么多关于字符串的方法,妈妈再也不担心我在终端模式下不记得方法名了。
上面这特简单的几行代码还没包含类和对象,下面看看在swift
解析器中直接输入类的定义和对象创建和简单使用。
diveinedu@diveinedu-VirtualBox:~$ swift
Welcome to Swift version 2.2-dev (LLVM 46be9ff861, Clang 4deb154edc, Swift 778f82939c). Type :help for assistance.
1> struct Resolution {
2. var width = 0
3. var height = 0
4. }
5. class VideoMode {
6. var resolution = Resolution()
7. var interlaced = false
8. var frameRate = 0.0
9. var name: String?
10. func description()
11. {
12. print("name:\(name) frameRate:\(frameRate)")
13. }
14. }
15> let mode = VideoMode()
mode: VideoMode = {
resolution = {
width = 0
height = 0
}
interlaced = false
frameRate = 0
name = nil
}
16> mode.name = "1080p HD"
17> mode.frameRate = 30.0
18> mode.description()
name:Optional("1080p HD") frameRate:30.0
19>
这些都只是在swift
解析器中临时性的运行一些代码,如果我们需要新建.swift
格式文件然后编译成可执行二进制文件形式又要怎样做呢,同样很简单,我们可以用swiftc
这个命令来编译。 我们可以新建一个目录来存放swift代码文件,然后编辑一个test.swift
:
diveinedu@diveinedu-VirtualBox:~$ mkdir -p $HOME/swift/swiftcode
diveinedu@diveinedu-VirtualBox:~$ cd $HOME/swift/swiftcode
diveinedu@diveinedu-VirtualBox:~/swift/swiftcode$ gedit test.swift
当打开gedit文本编辑器后,输入上面的类和对象创建以及方法调用的代码,列出在下面
struct Resolution {
var width = 0
var height = 0
}
class VideoMode {
var resolution = Resolution()
var interlaced = false
var frameRate = 0.0
var name: String?
func description()
{
print("name:\(name) frameRate:\(frameRate)")
}
}
let mode = VideoMode()
mode.name = "1080p HD"
mode.frameRate = 30.0
mode.description()
保存后关闭编辑器,然后执行swiftc test.swift
来编译源文件,会出现如下链接错误:
diveinedu@diveinedu-VirtualBox:~/swift/swiftcode$ swiftc test.swift
<unknown>:0: error: link command failed with exit code 127 (use -v to see invocation)
diveinedu@diveinedu-VirtualBox:~/swift/swiftcode$
解决办法是安装编译依赖clang libicu-dev
,输入下面命令回车(会询问当前用户密码)
diveinedu@diveinedu-VirtualBox:~/swift/swiftcode$ sudo apt-get install clang libicu-dev
安装完成后再次执行编译命令swiftc test.swift
就顺利编译成功,再当前目录下输出test
可执行文件。
diveinedu@diveinedu-VirtualBox:~/swift/swiftcode$ swiftc test.swift
diveinedu@diveinedu-VirtualBox:~/swift/swiftcode$ ./test
name:Optional("1080p HD") frameRate:30.0
而且执行ldd ./test
查看此二进制文件依赖的动态库可知,它链接了libswiftCore
,这是所有swift
程序都会需要的。
diveinedu@diveinedu-VirtualBox:~/swift/swiftcode$ ldd ./test
linux-vdso.so.1 => (0x00007ffcef3f5000)
libswiftCore.so => /home/diveinedu/swift/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu15.10/usr/lib/swift/linux/libswiftCore.so (0x00007f1cd2f75000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f1cd2bdd000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f1cd28d5000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f1cd26be000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f1cd22f3000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f1cd20d5000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f1cd1ed1000)
libicuuc.so.55 => /usr/lib/x86_64-linux-gnu/libicuuc.so.55 (0x00007f1cd1b3c000)
libicui18n.so.55 => /usr/lib/x86_64-linux-gnu/libicui18n.so.55 (0x00007f1cd16d9000)
libbsd.so.0 => /lib/x86_64-linux-gnu/libbsd.so.0 (0x00007f1cd14c9000)
/lib64/ld-linux-x86-64.so.2 (0x0000556e488b7000)
libicudata.so.55 => /usr/lib/x86_64-linux-gnu/libicudata.so.55 (0x00007f1ccfa11000)
细心的读者会发现好像不见main
函数或者main
相关的函数,程序照样可以运行,不管是脚本还是编译成二进制可执行文件,这个我以后再细说了。
The Swift Programming Language 2.2