1. Preface
I have been busy these two days and haven’t had time to record anything. At around 1 am on Wednesday, December 4th, I saw that Apple officially open sourced Swift. Major foreign media information moved very quickly. I also got excited and read the latest news about Swift open source. As we all know, the Swift language for the Apple platform has been out for a year and a half, and it has been growing and going through several versions. The open source of Swift that many people are looking forward to is that this new language can be used on platforms other than the Apple platform. For example, some people hope to be able to use Swift or do background development in the future. In theory, there is no problem, but there are also people who will complain about such a vision, but once open source is released, more or less community members will go to this aspect. To work hard. Apple officially launched a new website for Swift, swift.org, and also released the pre-compiled Swift tool chain packaging files for the Ubuntu Linux platform and the compilation guide under Linux. The purpose of writing this blog today is to introduce and promote the use of Swift language among beginners or junior college students.
2.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. For virtual machines, it is recommended to use VirtualBox. Swift supports two versions of Ubuntu 14.04 and 15.10. I chose 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
diveinedu@diveinedu-VirtualBox:~$ mkdir swift && cd swift; diveinedu@diveinedu-VirtualBox:~/swift$ wget https://swift.org/builds/ubuntu1510/swift-2.2-SNAPSHOT-2015-12-01-b/swift-2.2-SNAPSHOT-2015-12-01-b-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 it first, and then enter the directory. There will be subdirectories such as usr/bin and usr/lib in the directory:
diveinedu@diveinedu-VirtualBox:~/swift$ tar xvf swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu15.10.tar.gz diveinedu@diveinedu-VirtualBox:~/swift$ cd swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu15.10/
Then configure user-level environment variables and edit the $HOME/.bashrc configuration file
diveinedu@diveinedu-VirtualBox:~/swift/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu15.10$ gedit $HOME/.bashrc
The above command will call out Use 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-SNAPSHOT-2015-12-01-b-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
Then the environment variables are configured OK. At this time we only need to close our Shell terminal and reopen the terminal for it to take effect.
3. First experience with Swift+Ubuntu
Everyone who has done iOS development knows that when Swift was first born in June 2014, it came with the Playground function in Xcode. You can watch the running results while writing. Is it available under Ubuntu Linux? There is nothing similar, but there is, but it does not have such powerful IDE support. We can still run a Swift parser similar to the Pyhton script parser, and input Swift code synchronously to "parse" and run it. This command is swift. After setting the above environment variables, you can use it directly by reopening the terminal, 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
There is also an automatic prompt completion function in this analysis and execution interface! It’s like four countries. The fifth line above is to enter hello and then enter a little more. Then press the tab key, and so many methods about strings will appear at once. My mother no longer worries that I can’t remember the method names in terminal mode.
The above simple lines of code do not include classes and objects. Let’s take a look at how to directly enter the class definition and object creation and simple use in the swift parser.
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>
These are just some codes that are temporarily run in the swift parser. What if we need to create a new .swift format file and then compile it into an executable binary file? It is also very simple. We can use the swiftc command to compile. We can create a new directory to store swift code files, and then edit a 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
After opening the gedit text editor, enter the above code for class and object creation and method calling, which are listed below
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()
Save Then close the editor, and then execute swiftc test.swift to compile the source file. The following link error will appear:
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$
The solution is to install the compilation dependency clang libicu-dev, enter the following command and press Enter (the current user password will be asked)
diveinedu@diveinedu-VirtualBox:~/swift/swiftcode$ sudo apt-get install clang libicu-dev
After the installation is completed, execute the compilation command swiftc test.swift again and the compilation will be successful, and the test executable file will be output in the current directory.
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相关的函数,程序照样可以运行,不管是脚本还是编译成二进制可执行文件,这个我以后再细说了。