Blame view

HDFwear/Home/Model/NewBeiDouContactModel.swift 1.93 KB
ba0a04ea   jason   feat:string half ...
1
2
3
4
5
6
7
8
  //
  //  NewBeiDouContactModel.swift
  //  HDFwear
  //
  //  Created by admin on 2023/11/17.
  //
  
  import UIKit
9b1c370b   jason   feat:beidou
9
10
11
12
13
14
15
  import HandyJSON
  import SwiftDate
  
  enum NewBeiDouContactType: Int, HandyJSONEnum {
      case emergency = 1
      case general = 2
  }
ba0a04ea   jason   feat:string half ...
16
17
  
  class NewBeiDouContactModel: NSObject {
9b1c370b   jason   feat:beidou
18
19
20
21
22
      required override init() { }
      
      var name: String = ""
      var phone: String = ""
      var type: NewBeiDouContactType = .general
35756df0   jason   feat:0301 part1
23
24
25
26
      
      override var description: String {
           return "Name: \(name), Phone: \(phone), Type: \(type.rawValue)"
       }
ba0a04ea   jason   feat:string half ...
27
  
9b1c370b   jason   feat:beidou
28
29
30
31
32
      init(name: String, phone: String, type: NewBeiDouContactType) {
          self.name = name
          self.phone = phone
          self.type = type
      }
35756df0   jason   feat:0301 part1
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
      
      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
      }
  
  
ba0a04ea   jason   feat:string half ...
75
  }