NewBeiDouContactModel.swift
1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// NewBeiDouContactModel.swift
// HDFwear
//
// Created by admin on 2023/11/17.
//
import UIKit
import HandyJSON
import SwiftDate
enum NewBeiDouContactType: Int, HandyJSONEnum {
case emergency = 1
case general = 2
}
class NewBeiDouContactModel: NSObject {
required override init() { }
var name: String = ""
var phone: String = ""
var type: NewBeiDouContactType = .general
override var description: String {
return "Name: \(name), Phone: \(phone), Type: \(type.rawValue)"
}
init(name: String, phone: String, type: NewBeiDouContactType) {
self.name = name
self.phone = phone
self.type = type
}
class func getNewBeiDouContact(from data: [UInt8]) -> [NewBeiDouContactModel]? {
var index = 0
var contacts: [NewBeiDouContactModel] = []
guard data.count >= 1 else { return nil }
let contactsCount = Int(data[index])
index += 1
for _ in 0..<contactsCount {
guard index + 2 < data.count else { return nil }
let nameLength = Int(data[index])
index += 1
let nameData = Array(data[index..<(index + nameLength)])
guard let name = String(bytes: nameData, encoding: .utf8) else { return nil }
index += nameLength
let phoneLength = Int(data[index])
index += 1
let phoneData = Array(data[index..<(index + phoneLength)])
guard let phone = String(bytes: phoneData, encoding: .ascii) else { return nil }
index += phoneLength
let typeRawValue = data[index]
index += 1
if let type = NewBeiDouContactType(rawValue: Int(typeRawValue)) {
let contact = NewBeiDouContactModel(name: name, phone: phone, type: type)
contacts.append(contact)
} else {
return nil
}
}
return contacts
}
}