- 苹果公司在 3.29 正式发布了正式版的
Xcode 9.3
和Swift 4.1
, 让我们看看Swift 4.1
带来了哪些新功能和新亮点 - 测试需要Xcode9.3, 请确保你的Xcode是最新的9.3版本
Swift 4.1
与Swift 4.0
是源代码兼容的,所以如果你已经使用Xcode
中的Swift Migrator
将你的项目迁移到Swift 4.0
,那么新特性不会破坏你的代码- 下面在
Xcode9.3
下新建一个Playground
工程, 测试我们的代码
条件一致性(Conditional Conformance)
- 条件一致性使类型参数满足特定条件的泛型类型的协议一致性
- 在Swift 4中,如果数组、字典或者可选类型的元素类型遵循
Equatable
,你是可以在数组之间、字典之间和可选类型之间进行比较的, 如下示例:
// Int类型的数组let arr1 = [1, 2, 3]let arr2 = [1, 2, 3]print(arr1 == arr2)// 比较value值为Int的字典let dic1 = ["age": 19, "score": 60]let dic2 = ["age": 19, "score": 60]print(dic1 == dic2)// 比较Int?let age1 = dic1["age"]let age2 = dic2["age"]print(age1 == age2)/// 以上输出结果都是: true复制代码
这里如果我们把Int
都换成Int?
类型, 在Swift4.0
中是不能编译通过的, 如下:
// Int类型的数组let arr1: [Int?] = [1, 2, 3]let arr2: [Int?] = [1, 2, 3]print(arr1 == arr2)// 比较value值为Int的字典let dic1: [String: Int?] = ["age": 19, "score": 60]let dic2: [String: Int?] = ["age": 19, "score": 60]print(dic1 == dic2)// 比较Int?let age1 = dic1["age"]let age2 = dic2["age"]print(age1 == age2)复制代码
- 在这些实例中, 我们用
==
测试相等性, 在Swift4.0中,Int
类型遵循Equatable
协议, 可以比较, 但是Int?
类型却没有遵循Equatable
协议 - 但是在Swift4.1中, 完美的解决了这个问题, 上述代码可比那已通过, 且都输出:
true
- 在
Swift 4.0
中[Set<Int>]
之间可以直接对比,但是[[Int]]
不能。现在Swift 4.1
中,[[Int]]
也能直接对比 - 总的来说,
Swift 4.1
的Array
、Dictionary
和Optional
,只要他们的元素都遵循了Equatable
和Hashable
,那么他们也遵循Equatable
和Hashable
合成 Equatable
和 Hashable
- 如果对象相等,则这两个对象的
hash
值一定相等 - 如果两个对象
hash
值相等,这两个对象不一定相等。 Swift
中Hashable
一定是Equatable
,因为前者继承了后者。 在Swift 4
中,若遵循Equatable
协议的时候,我们必须实现Equatable
协议的==
方法,Equatable
协议如下:
public protocol Equatable { /// Returns a Boolean value indicating whether two values are equal. /// /// Equality is the inverse of inequality. For any values `a` and `b`, /// `a == b` implies that `a != b` is `false`. /// /// - Parameters: /// - lhs: A value to compare. /// - rhs: Another value to compare. public static func == (lhs: Self, rhs: Self) -> Bool}复制代码
在Swift4.0中, 必须实现Equatable
协议的方法
struct Name: Equatable { var name1 = "name1" var name2 = "name2" static func == (lhs: Name, rhs: Name) -> Bool { return lhs.name1 == rhs.name1 && lhs.name2 == rhs.name2 }}复制代码
在Swift 4.1
,只需要加上Equatable
即可, 不需要实现任何协议方法
struct Name: Equatable { var name1 = "name1" var name2 = "name2" }复制代码
JSON
编码时支持Camel Case
和Snake Case
之间的转换
Swift 4.0
引入了Codable
,但是有个麻烦的问题:如果JSON
数据的key
命名格式是snake_case
的话,我们必须创建自己的CodingKeys
来告诉苹果怎么转换。在Swift 4.0
中- 但是在
Swift 4.1
中,苹果给JSONDecoder
引入了一个属性keyDecodingStrategy
;对应的JSONEncoder
引入了一个属性keyEncodingStrategy
。这样我们就不需要设置定义CodingKeys
了。只需要在decoding
的时候把keyDecodingStrategy
设置为.convertFromSnakeCase
;在encoding
的时候把keyEncodingStrategy
设置为.convertToSnakeCase
- 下面是分别针对数组/字典/集合的解析形式
struct Student: Codable, Hashable { let firstName: String let averageGrade: Int}let cosmin = Student(firstName: "Cosmin", averageGrade: 10)let george = Student(firstName: "George", averageGrade: 9)let encoder = JSONEncoder()// Encode an Array of studentslet students = [cosmin, george]do { try encoder.encode(students)} catch { print("Failed encoding students array: \(error)")}// Encode a Dictionary with student valueslet studentsDictionary = ["Cosmin": cosmin, "George": george]do { try encoder.encode(studentsDictionary)} catch { print("Failed encoding students dictionary: \(error)")}// Encode a Set of studentslet studentsSet: Set = [cosmin, george]do { try encoder.encode(studentsSet)} catch { print("Failed encoding students set: \(error)")}// Encode an Optional Studentlet optionalStudent: Student? = cosmindo { try encoder.encode(optionalStudent)} catch { print("Failed encoding optional student: \(error)")}复制代码
Hashable Index Types
(哈希化索引)
扩展 Key-path
表达式在标准库中的使用范围。让标准库中所有的索引类型都符合 Hashable
协议,这样,[Int]
、String
和所有其它标准集合使用 key-path
下标时,表现都是一样的
let swiftString2 = "one two three"let charact1 = \String.[swiftString2.startIndex]print(swiftString2[keyPath: charact1])let arr = [1, 2, 3, 4]let value2 = \[Int].[1]print(arr[keyPath: value2])//输出结果:o2复制代码
compactMap
的用法
在Swift 4.0
中,我们经常使用flatMap
来过滤nil
,也可以进行降维操作, 详情可参考
let arr = [1, 2, nil, 3, 4, nil]let arr1 = arr.flatMap({ $0 })print(arr1)//这样使用会有类似的警告'flatMap' is deprecated: Please use compactMap(_:) for the case where closure returns an optional value Use 'compactMap(_:)' instead//Swift4.1中的用法let arr = [1, 2, nil, 3, 4, nil]let arr2 = arr.compactMap({ $0 })print(arr2)复制代码
主要是因为在
Swift4.0
中flatMap
有很多重载, 可能会引起歧义, 所以在Swift4.1
中把flatMap
重命名为compactMap
除了协议中的 weak 和 unowned。
- 当你在
Tune
协议中定义了两个属性key
和pitch
,pitch
可能为nil
, 所以你在协议中可以用weak
修饰 - 但是如果在协议本身中定义的话,
weak
和unowned
都没有实际意义, 所以在Swift4.1
中就已经去掉了这些关键字, 并且在协议中使用这些关键字将会爆出警告
class Key {}class Pitch {}// Swift 4protocol Tune { unowned var key: Key { get set } weak var pitch: Pitch? { get set }}// Swift 4.1protocol Tune { var key: Key { get set } var pitch: Pitch? { get set }}复制代码
UnsafeMutableBufferPointer
的改变
//Swift4.0let buffer = UnsafeMutableBufferPointer (start: UnsafeMutablePointer .allocate(capacity: 10), count: 10)let mutableBuffer = UnsafeMutableBufferPointer(start: UnsafeMutablePointer(mutating: buffer.baseAddress), count: buffer.count) //Swift4.1let buffer = UnsafeMutableBufferPointer .allocate(capacity: 10)let mutableBuffer = UnsafeMutableBufferPointer(mutating: UnsafeBufferPointer(buffer))复制代码
相对
Swift4.0
的改变,Swift4.1
这点改变键值太微不足道了, 传说中Swift5
的API
会趋于稳定, 但是估计改变可能也是非常大的, 坐等Swift5
发布...