去gayhub上浏览大佬的代码
struct Landmark: Hashable, Codable, Identifiable {
...
}
此时萌新的表情是这样的
奥不对,是这样的
今天我们就仔细分析下这几个。。。是干啥用的
首先 - Hashable
You can use any type that conforms to the Hashable protocol in a set or as a dictionary key.
只有遵循了Hashable 协议 才能被添加到 Set 中 或者用作 Dictionary 的 key 值
举个例子
// 假如我们有一个Person类
struct Person {
var name: String
var age: Int
}
// 假如我们有两位同学 小明 和 小红
let ming = Person(name: "ming", age: 10)
let hong = Person(name: "hong", age: 11)
// 现在我们有一个需求 1.把他加入到一个Set中 2. 用作Dictionary的key
// 我们这样写
var set: Set = [] //报错 Type 'Person' does not conform to protocol 'Hashable'
set.insert(ming)
set.insert(hong)
print(set)
var dic: [Person: String] = [:] //报错 Type 'Person' does not conform to protocol 'Hashable'
dic[ming] = ming.name //报错 Referencing subscript 'subscript(_:)' on 'Dictionary' requires that 'Person' conform to 'Hashable'
dic[hong] = hong.name //报错 Referencing subscript 'subscript(_:)' on 'Dictionary' requires that 'Person' conform to 'Hashable'
print(dic)
// 由于我们的Person并没有遵循Hashable协议,所以以上代码会报错
// 接下来 我们修改Person
struct Person: Hashable {
var name: String
var age: Int
}
// 修改之后 ,以上报错会消失
// 打印结果为 set ---- [Test0.Person(name: "hong", age: 11), Test0.Person(name: "ming", age: 10)]
// dic --- [Test0.Person(name: "ming", age: 10): "ming", Test0.Person(name: "hong", age: 11): "hong"]
- 拓展
// 假如我们的Person是一个Class而不是Struct,就必须要实现Hashable的协议方法
class Person: Hashable {
// Hashable 继承自 Equatable, 此方法为Equatable的协议方法, 用来比较两个对象是否协议相等, 返回true的话 在Set或者Dictionary中都会被认为是同一个对象(在Set中会被去重, 在Dictionary中覆盖之前的值)
static func == (lhs: Person, rhs: Person) -> Bool {
return lhs.name == rhs.name && lhs.age == rhs.age
}
func hash(into hasher: inout Hasher) {
hasher.combine(name)
hasher.combine(age)
}
init(name: String, age: Int) {
self.name = name
self.age = age
}
var name: String
var age: Int
}
举个栗子
// 还是之前的小明和小红
let ming = Person(name: "ming", age: 10)
let hong = Person(name: "hong", age: 11)
var set: Set = []
set.insert(ming)
set.insert(hong)
print(set) // [Test0.Person, Test0.Person]
var dic: [Person: String] = [:]
dic[ming] = ming.name
dic[hong] = hong.name
print(dic) // [Test0.Person: "hong", Test0.Person: "ming"]
// 因为ming1.name == ming.name && ming1.age == ming.age Person 的 static func == (lhs: Person, rhs: Person) -> Bool 方法成立 ming1 会覆盖之前的 ming
let ming1 = Person(name: "ming", age: 10)
set.insert(ming1)
dic[ming1] = ming1.name
print(set) // [Test0.Person, Test0.Person]
print(dic) // [Test0.Person: "hong", Test0.Person: "ming"]
// // 因为ming2.name == ming1.name && ming2.age == ming1.age Person 的 static func == (lhs: Person, rhs: Person) -> Bool 方法不成立 ming2 不会会覆盖之前的 1ming
let ming2 = Person(name: "ming", age: 20)
set.insert(ming2)
dic[ming2] = ming2.name
print(set) // [Test0.Person, Test0.Person, Test0.Person]
print(dic) // [Test0.Person: "hong", Test0.Person: "ming", Test0.Person: "ming"]
假如我们对Person进行修改 , 年龄相同就当做同一个人
class Person: Hashable {
static func == (lhs: Person, rhs: Person) -> Bool {
// return lhs.name == rhs.name && lhs.age == rhs.age
return lhs.age == rhs.age
}
func hash(into hasher: inout Hasher) {
// hasher.combine(name)
hasher.combine(age)
}
init(name: String, age: Int) {
self.name = name
self.age = age
}
var name: String
var age: Int
}
// 还是之前的小明和小红
let ming = Person(name: "ming", age: 10)
let hong = Person(name: "hong", age: 11)
var set: Set = []
set.insert(ming)
set.insert(hong)
print(set) // [Test0.Person, Test0.Person]
var dic: [Person: String] = [:]
dic[ming] = ming.name
dic[hong] = hong.name
print(dic) // [Test0.Person: "hong", Test0.Person: "ming"]
// 因为ming1.age == ming.age Person 的 static func == (lhs: Person, rhs: Person) -> Bool 方法成立 ming1 会覆盖之前的 ming
let ming1 = Person(name: "ming", age: 10)
set.insert(ming1)
dic[ming1] = ming1.name
print(set) // [Test0.Person, Test0.Person]
print(dic) // [Test0.Person: "hong", Test0.Person: "ming"]
// // 因为 ming2.age == ming1.age Person 的 static func == (lhs: Person, rhs: Person) -> Bool 方法成立 ming2 会覆盖之前的 1ming
let ming2 = Person(name: "aming", age: 10)
set.insert(ming2)
dic[ming2] = ming2.name
print(set) // [Test0.Person, Test0.Person, Test0.Person]
print(dic) // [Test0.Person: "hong", Test0.Person: "aming"]
其次 - Codable
在swift4之前,swift中数据解析的方式大都采用OC的KVC机制,swift4后可以采用Codable直接将json转成对象
先看定义
public typealias Codable = Decodable & Encodable
json转对象
struct Person: Codable {
var name: String
var age: Int
}
let jsonStr = "{\"name\" : \"ming\", \"age\" : 10}"
guard let jsonData = jsonStr.data(using: .utf8) else {
exit(0)
}
let decoder = JSONDecoder()
guard let obj = try? decoder.decode(Person.self, from: jsonData) else {
exit(0)
}
print(obj.name) // ming
print(obj.age) // 10
当json中的key值与我们定义的属性名对应时可以采用上述简单的方法, 如果不对应的话就需要借助CodingKeys来实现
// 修改Person
struct Person: Codable {
var name: String
var age: Int
enum CodingKeys: String, CodingKey {
case name = "a_name"
case age = "age" //如果前后一致的话 可以省略=以及后边的部分 如 case age, 如果有需要忽略的key, 则不写到此处便可
}
}
let decoder = JSONDecoder()
let jsonStr1 = "{\"a_name\" : \"ming\", \"age\" : 10}"
guard let jsonData1 = jsonStr1.data(using: .utf8) else {
exit(0)
}
guard let obj1 = try? decoder.decode(Person.self, from: jsonData1) else {
exit(0)
}
print(obj1.name) // ming
print(obj1.age) // 10
第三 - CaseIterable
swift4.2之后引进CaseIterable, 用于合成简单枚举类型的allCases静态属性
举个栗子
enum Week: CaseIterable {
case Sun
case Mon
case Tue
case Wen
case Thu
case Fri
case Sat
}
print(Week.allCases) // [Test0.Week.Sun, Test0.Week.Mon, Test0.Week.Tue, Test0.Week.Wen, Test0.Week.Thu, Test0.Week.Fri, Test0.Week.Sat]
我们也可以重写allCases方法 例如
enum Week: CaseIterable {
case Sun
case Mon
case Tue
case Wen
case Thu
case Fri
case Sat
static var allCases: [Week] {
return [.Mon, .Tue, .Wen, .Thu, .Fri, Sat, Sun]
}
}
print(Week.allCases) //[Test0.Week.Mon, Test0.Week.Tue, Test0.Week.Wen, Test0.Week.Thu, Test0.Week.Fri, Test0.Week.Sat, Test0.Week.Sun]
ming
至于 - Identifiable
完全没搞懂呀, 仅仅是遵循了这个协议之后就必须有一个名为id的属性吗?然后呢。。。