- 苹果公司在 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)
- 条件一致性使类型参数满足特定条件的泛型类型的协议一致性 [ SE-0143 ]
- 在Swift 4中,如果数组、字典或者可选类型的元素类型遵循
,你是可以在数组之间、字典之间和可选类型之间进行比较的, 如下示例:Equatable
// Int类型的数组
let arr1 = [, , ]
let arr2 = [, , ]
print(arr1 == arr2)
// 比较value值为Int的字典
let dic1 = ["age": , "score": ]
let dic2 = ["age": , "score": ]
print(dic1 == dic2)
// 比较Int?
let age1 = dic1["age"]
let age2 = dic2["age"]
print(age1 == age2)
/// 以上输出结果都是: true
复制代码
这里如果我们把
Int
都换成
Int?
类型, 在
Swift4.0
中是不能编译通过的, 如下:
// Int类型的数组
let arr1: [Int?] = [, , ]
let arr2: [Int?] = [, , ]
print(arr1 == arr2)
// 比较value值为Int的字典
let dic1: [String: Int?] = ["age": , "score": ]
let dic2: [String: Int?] = ["age": , "score": ]
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
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
之间的转换
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: )
let george = Student(firstName: "George", averageGrade: )
let encoder = JSONEncoder()
// Encode an Array of students
let students = [cosmin, george]
do {
try encoder.encode(students)
} catch {
print("Failed encoding students array: \(error)")
}
// Encode a Dictionary with student values
let studentsDictionary = ["Cosmin": cosmin, "George": george]
do {
try encoder.encode(studentsDictionary)
} catch {
print("Failed encoding students dictionary: \(error)")
}
// Encode a Set of students
let studentsSet: Set = [cosmin, george]
do {
try encoder.encode(studentsSet)
} catch {
print("Failed encoding students set: \(error)")
}
// Encode an Optional Student
let optionalStudent: Student? = cosmin
do {
try encoder.encode(optionalStudent)
} catch {
print("Failed encoding optional student: \(error)")
}
复制代码
Hashable Index Types
(哈希化索引)
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 = [, , , ]
let value2 = \[Int].[]
print(arr[keyPath: value2])
//输出结果:
o
复制代码
compactMap
的用法
compactMap
在
Swift 4.0
中,我们经常使用
flatMap
来过滤
nil
,也可以进行降维操作, 详情可参考Swift函数式编程之高级用法
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 4
protocol Tune {
unowned var key: Key { get set }
weak var pitch: Pitch? { get set }
}
// Swift 4.1
protocol Tune {
var key: Key { get set }
var pitch: Pitch? { get set }
}
复制代码
UnsafeMutableBufferPointer
的改变
UnsafeMutableBufferPointer
//Swift4.0
let buffer = UnsafeMutableBufferPointer<Int>(start: UnsafeMutablePointer<Int>.allocate(capacity: ),
count: )
let mutableBuffer = UnsafeMutableBufferPointer(start: UnsafeMutablePointer(mutating: buffer.baseAddress),
count: buffer.count)
//Swift4.1
let buffer = UnsafeMutableBufferPointer<Int>.allocate(capacity: )
let mutableBuffer = UnsafeMutableBufferPointer(mutating: UnsafeBufferPointer(buffer))
复制代码
相对的改变,
Swift4.0
这点改变键值太微不足道了, 传说中
Swift4.1
的
Swift5
会趋于稳定, 但是估计改变可能也是非常大的, 坐等
API
发布...
Swift5
参考文档
- What’s New in Swift 4.1?