美文网首页
译文《What’s New in Swift 5?》

译文《What’s New in Swift 5?》

作者: superKelly | 来源:发表于2019-03-28 15:46 被阅读0次

《What’s New in Swift 5?》

Language Improvements

  • 支持 isMultiple(of:)
  • 使用##来显示反斜杠与“”
  • 支持isNumber来判断字符是否为数字
  • 支持isAlphabetic
  • 去掉Subsequences

Dictionary

支持compactMapValues
DictionaryLiteral 改名为 KeyValuePairs

插入字符方法更新

swift4.2 组装string的步骤:将每个string包装成stringInterpolationSegment,再将所有stringInterpolationSegment包装成stringInterpolationSegment,形成新的string。

let language = "Swift"
let languageSegment = String(stringInterpolationSegment: language)
let space = " "
let spaceSegment = String(stringInterpolationSegment: space)
let version = 4.2
let versionSegment = String(stringInterpolationSegment: version)
let string = String(stringInterpolation: languageSegment, spaceSegment, versionSegment)

swift5
1.生成DefaultStringInterpolation
2.appendLiteral string

  1. stringInterpolation 生成新的string
// 1
var interpolation = DefaultStringInterpolation(
  literalCapacity: 7,
  interpolationCount: 1)
// 2
let language = "Swift"
interpolation.appendLiteral(language)
let space = " "
interpolation.appendLiteral(space)
let version = 5
interpolation.appendInterpolation(version)
// 3
let string = String(stringInterpolation: interpolation)

将switch中的default 改名为@ unknown

新增 Result

Never 支持 Equatable 和 Hashable

增加dynamically callable types来与Python或Ruby等脚本语言互操作 dynamicallyCall

Package Manager更新

Platform Deployment Settings ,可指定minimum required platform deployment target version。如.iOS(.v12),
Target Build Settings,可定制化
Dependency Mirroring,新增
Making Codable Ranges,
Flattening Nested Optionals:try? in Swift 5 doesn’t create nested optionals
Removing Customization Points From Collections:移除了collection的Customization,因为容易产生理解错误。

  • Identity Key Paths:Swift 4.2 通过 .self 来访问value
tutorial.self = Tutorial(title: "What's New in Swift 5?", author: "Cosmin Pupăză")

swift5 增加identity key paths。

tutorial[keyPath: \.self]= Tutorial(title: "What's New in Swift 5?", author: "Cosmin Pupăză")
  • Build Configuration Updates
    增加 < ,可替代 !swift(>=5)

  • Using Variadic Parameters for Enumeration Cases With Associated Values
    swift4.2 枚举值可以这样写 case tutorial(_: String...)
    swift5 中,不再支持,取代为case tutorial([String])

  • Deprecating String Index Encoded Offsets
    swift 4.2 对string做UTF-16 编码。swift5则是UTF8.
    encodedOffset取代为 utf16Offset(in:)

总结

Swift 5 增加了ABI稳定,因此,今后的版本不会再有太大的改变。
更多内容可参考 CHANGELOGSwift standard library diffs

相关文章

网友评论

      本文标题:译文《What’s New in Swift 5?》

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