美文网首页
What's new in Swift 3.1

What's new in Swift 3.1

作者: JellyL | 来源:发表于2017-04-11 13:03 被阅读33次

    2017年3月27日,Apple官方发布了Swift 3.1。Swift 3.1 是一个小版本的更新,主要包含了包含了标准库的改进和完善,以及一些期待已久的包管理功能。

    Swift 3.1 兼容Swift 3.0,如果你的项目已更新到Swift 3.0,这次更新不会给你带来太多问题。主要的更新包括:Language Updates 和 Package Manager Updates两个部分。

    Language Updates#

    Improved numeric conversion initializers###

    为所有的数字类型(Int, Int8, Int16, Int32, Int64, UInt, UInt8, UInt16, UInt32, UInt64, Float, Float80, Double) 定义了Failable Numeric Conversion Initializers,使用这个可失败数字转换构造方法进行构造,成功时,返回没有失去精度的值,失败时,会返回空。

    解决的问题:能够更安全的进行数字转换。
    适用场景如解析服务器返回的json数据。

    let a = 2.33333
    let b = Int(a) //2  丢失精度
    let c = Int(exactly: a) //nil  当有精度丢失时,返回空  <-- 3.1 feature here
    
    let e = 6.0
    let f = Int(exactly: e) //Optional(6) 无精度丢失,成功返回 <-- 3.1 feature 
    

    New Sequence protocol members###

    Sequence中新添加了两个方法prefix(while:)drop(while:),重写了CollectionLazySequenceProtocolLazyCollectionProtocol

    protocol Sequence {
      // ...
      /// Returns a subsequence by skipping elements while `predicate` returns
      /// `true` and returning the remainder.
      func drop(while predicate: (Self.Iterator.Element) throws -> Bool) rethrows -> Self.SubSequence
      /// Returns a subsequence containing the initial elements until `predicate`
      /// returns `false` and skipping the remainder.
      func prefix(while predicate: (Self.Iterator.Element) throws -> Bool) rethrows -> Self.SubSequence
    }
    

    解决的问题:更有效的在序列中筛选元素,特别是在无限序列中筛选元素。

    Availability by Swift version###

    为了达到版本控制的目的,可以这样

    #if swift(>= 3.1) {
      //Swift 3.1 api
    } elseif swift(>= 3.0) {
      //Swift 3.0 api
    } 
    #endif 
    

    但是编译器会为它支持的每一个Swift版本都独立编译一遍。

    通过available约束Swift版本,Swift 3.1中对 @available进行了扩展,不仅可以用它限定操作系统版本,还可以指定Swift的版本。

    解决的问题:当通过@available这样的形式达到版本控制来编写程序库时,只需要编译一次,就可以在程序库中包含每个API可以支持的Swift版本。

    @available(swift, introduced: 3.0, obsoleted: 3.1)
    func xxx()
    
    • introduced: 表示最低支持的版本
    • obsoleted: 表示API过期版本

    Make non-escaping closures the default###

    Swift 3.0 SE-0103开始将传递给function的closure类型的参数默认为non-escaping.
    但是在Swift 3.1中你可以通过withoutActuallyEscaping()方法将non-escapingclosure暂时转变为escaping.

    func withoutActuallyEscaping<ClosureType, ResultType>(
        _ closure: ClosureType,
        do: (fn: @escaping ClosureType) throws -> ResultType) rethrows -> ResultType {
      // ...
    }
    

    解决的问题: 当需要把non-escaping属性的closure,传递给需要escaping属性closure的函数时产生的编译错误。

    func xxx(_ fn: () -> Void, on queue: DispatchQueue) {
      withoutActuallyEscaping(fn) { escapableFN in    
          queue.async(execute: escapableFN)           
      }
    }
    
    • fnnon-escaping,并被转换成escapingescapableFN.
    • async(execute:)需要 escaping closure.
    • withoutActuallyEscaping限定了escapableFN的使用范围.

    Package Manager Updates#

    包管理器添加了可编辑的包(Editable Packages),版本锁定(Version pinning),包管理器工具版本(Tools version)以及Swift语言兼容性版本(Swift language compatibility version)的功能。

    Editable packages###

    通过swift build --edit <NAME>命令让软件包变为可编辑的。
    通过swift build --end-edit <NAME>命令包管理器还原回规范解析的包。

    这意味着包将下移至用户的Package目录,可从依赖项更新过程中排除,因此用户可以更自由地提交并推送变更。

    Version pinning###

    版本锁定是指独立于语义版本规范,依赖关系解析算法精确地控制依赖关系进行选择特定版本。 包管理器通过锁定版本可以在遵守依赖约束的同时选择的所有版本的包中选择特定版本的方式。
    使用swift package pinswift package unpin命令,或编辑Package.pins文件实现锁定包版本。

    $ swift package pin --all      // 锁定所有依赖
    $ swift package pin <NAME>        // 把包名为Name的包锁定在当前解析版本 
    $ swift package pin <NAME> --version x.x.x  // 把包名为Name的包锁定在版本x.x.x
    $ swift package unpin ( [--all] | [<package-name>] ) //解除版本锁定
    

    还提供了一个message参数,用来记录固定依赖的原因。

    $ swift package pin Foo --message "The patch updates for xxx are really unstable and need screening."
    

    Tools version###

    让软件包在不破坏使用老版本工具链客户端的情况下采用Swift的新功能。在更新老版本工具链的软件包时,需要较新功能的软件包版本可被自动忽略。

    Swift language compatibility version###

    与上一个功能的意义类似,但主要针对语言的版本,可决定软件包要使用第3或第4版语言。

    Other Package Manager improvements###

    swift package reset命令将包重置为干净状态,不会检出当前的依赖关系或 build artifact。
    swift test --parallel命令并行执行测试。

    参考文献:
    swift-3-1-released
    All Swift Evolution Proposals
    What's new in Swift 3.1

    相关文章

      网友评论

          本文标题:What's new in Swift 3.1

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