美文网首页
Swift3.0学习笔记(一)

Swift3.0学习笔记(一)

作者: Felix_Smile | 来源:发表于2016-09-17 01:29 被阅读863次

    Swift 3.0学习笔记(一)

    开始

    对于英语比较好的读者,可以直接阅读苹果官方的文档。链接

    编译环境

    swift的专门的编译工具是Xcode,Xcode现在已经更新到了8.0版本。不够,Xcode只能在苹果的MacOS系统下安装运行。在今年的WWDC大会上,苹果为大多数没有苹果笔记本的开发者带来了福音,苹果在推出swift2.3的同时,宣布Xcode能在Linux的环境下安装运行。
    Linux环境下的安装方式:

    $ sudo apt-get install clang 
    $ export PATH=/path/to/Swift/usr/bin:"${PATH}" //添加环境变量
    

    REPL编译运行swift

    REPL即交互式编译环境。我们在安装了Xcode后,可以使用终端来编译运行简单的swift代码。

    $ 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中,数据类型的首字母需要大写,这一点与其他的编程语言有所不同。

    > let greeting="Hello"
    > print(greeting)
    Hello
    

    上面的代码中,我们使用let关键字定义了一个常量。不过,这里我们并有声明常量的数据类型,这就体现了swift的强大之处,即类型推断。所谓的类型推断就是我们在定义常量,变量的时候可以不用声明数据的类型,它会帮我们自动进行推断。

    REPL还有一个强大之处就是函数和方法的显示。

    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. } 
    

    REPL导入包

     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
        ......
    

    该命令可以查看包的帮助信息。

    创建包

    $ 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 。

    使用LLDB调试器

    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);

    相关文章

      网友评论

          本文标题:Swift3.0学习笔记(一)

          本文链接:https://www.haomeiwen.com/subject/xyiiettx.html