对于英语比较好的读者,能够直接阅读苹果官方的文档。[连接] (https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html#//apple_ref/doc/uid/TP40014097-CH2-ID1 )html
swift的专门的编译工具是Xcode,Xcode如今已经更新到了8.0版本。不够,Xcode只能在苹果的MacOS系统下安装运行。在今年的WWDC大会上,苹果为大多数没有苹果笔记本的开发者带来了福音,苹果在推出swift2.3的同时,宣布Xcode能在Linux的环境下安装运行。 Linux环境下的安装方式:java
$ sudo apt-get install clang $ export PATH=/path/to/Swift/usr/bin:"${PATH}" //添加环境变量
REPL即交互式编译环境。咱们在安装了Xcode后,可使用终端来编译运行简单的swift代码。macos
$ swift --version Apple Swift version 3.0 (swiftlang-800.0.46.2 clang-800.0.38) Target: x86_64-apple-macosx10.9
从我终端使用命令运行后的结果能够知道,我如今的swift已经到了3.0版本。 使用REPL可以让初学者体验swift的编程过程。编程
> 1 + 2 $RO: Int = 3
上面的代码中,实现了简单的运算。运行的结果出现了Int关键字,它是一种数据类型。须要注意的是在swift中,数据类型的首字母须要大写,这一点与其余的编程语言有所不一样。swift
> let greeting="Hello" > print(greeting) Hello
上面的代码中,咱们使用let关键字定义了一个常量。不过,这里咱们并有声明常量的数据类型,这就体现了swift的强大之处,即类型推断。所谓的类型推断就是咱们在定义常量,变量的时候能够不用声明数据的类型,它会帮咱们自动进行推断。app
REPL还有一个强大之处就是函数和方法的显示。dom
5>“Hi”.re Available completions: 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 6> "Hi".re
在上面的代码中,在字符串Hi后输入了方法的前两个字母re,而后点击Tab键就会将与之相关的方法打印出来。 REPL是至关智能的,当咱们的代码中须要用到像循环这样的代码块时使用大括号{}能够实现自动换行。而且在代码中,循环语句是从**>到.**符号结束的部分。编程语言
10> let number=[1,2,3] number: [Int] = 3 values { [0] = 1 [1] = 2 [2] = 3 } 11> for n in numbers.reversed(){ 12. print(n) 13. }
14> import Darwin 15> arc4random_uniform(10) //随机生成一个10之内的数 $R1: UInt32 = 0 16> arc4random_uniform(10) //随机生成一个10之内的数 $R2: UInt32 = 6
$ swift package --help OVERVIEW: Perform operations on Swift packages ......
该命令能够查看包的帮助信息。ide
$ mkdir hello //建立名为hello的文件目录 $ cd hello //定位到建立的目录下
由于每一个包都须要有一个名为Package.swift的配置文件,因此咱们须要使用init命令对包进行初始化。函数
$ swift package init
被初始化后的包增长了Package.swift配置文件,Sources和Tests两个目录。Sources目录下有一个Hello.wift文件;Tests目录下有一个LinuxMain.swift文件和HelloTests目录,该目录下有一个HelloTests.swift文件。
$ swift build Compile Swift Module 'Hello' (1 sources)
$ swift test Compile Swift Module 'helloTests' (1 sources) Linking ./.build/debug/helloPackageTests.xctest/Contents/MacOS/helloPackageTests Test Suite 'All tests' started at 2016-09-17 00:24:44.838 Test Suite 'helloPackageTests.xctest' started at 2016-09-17 00:24:44.841 Test Suite 'helloTests' started at 2016-09-17 00:24:44.841 Test Case '-[helloTests.helloTests testExample]' started. Test Case '-[helloTests.helloTests testExample]' passed (0.005 seconds). Test Suite 'helloTests' passed at 2016-09-17 00:24:44.847. Executed 1 test, with 0 failures (0 unexpected) in 0.005 (0.005) seconds Test Suite 'helloPackageTests.xctest' passed at 2016-09-17 00:24:44.847. Executed 1 test, with 0 failures (0 unexpected) in 0.005 (0.006) seconds Test Suite 'All tests' passed at 2016-09-17 00:24:44.847. Executed 1 test, with 0 failures (0 unexpected) in 0.005 (0.009) seconds
编译成一个可执行的包
当包里面包含main.swift文件时,这个包就是可执行的。包文件管理器将编译这个文件为二进制的可执行文件。
$ mkdir Hello $ cd Hello $ swift package init --type executable $ swift build $ .build/debug/Hello Hello,World!
在上面的命令运行完以后,咱们发现Tests目录下是空的,这就是上面的建立的包的不一样。 另外,在Sources目录下生成的文件再也不是hello.swift而换成了main.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))")
使用上面的代码建立名为Factorial.swift的文件。使用一下命令行编译代码:
$ swiftc -g Factorial.java $ ls Factorial.dSYM Factorial.swift Factorial
swiftc 命令后加g参数生成一个调试信息,同时在当前目录下生成一个名为Factorial的可执行文件。
$ lldb Fatorial (lldb) target create "Factorial" Current executable set to 'Factorial' (x86_64). (lldb) b 2 (lldb) r (lldb) p n (lldb) c (lldb) br di
b 2:在第二行设置断点(breakpoint); r:运行(run)程序; p n:输出(print)参数n的值; c:继续(continue)运行; br di:断点(breakpoint)取消(disable)。