CLPlistTool.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // CLPlistTool.swift
  3. // SolarBT
  4. //
  5. // Created by weclouds on 2019/5/30.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. /*
  10. 保存数据
  11. BluetoothName:""
  12. struct BLEData {
  13. var device_name :String?
  14. var device_type :String?
  15. var device_model:String?
  16. var online: Bool? // 是否在线
  17. }
  18. 1、 搜索蓝牙设备 (蓝牙名称)
  19. 2、 连接蓝牙设备
  20. 3、 读取蓝牙设备部分信息 (型号,类型,名称 )
  21. 保存到plist
  22. */
  23. class CLPlistTool: NSObject {
  24. let BluetoothNameKey = "BluetoothName"
  25. let DeviceNameKey = "DeviceName"
  26. let DeviceTypeKey = "DeviceType"
  27. let DeviceModeKey = "DeviceMode"
  28. var filePath = "" // 文件路径
  29. static let sharedTool : CLPlistTool = {
  30. let tool = CLPlistTool()
  31. return tool
  32. }()
  33. //判断文件是否存在改plist ,不存在创建
  34. func createPlistFile() {
  35. //获取路径
  36. let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
  37. filePath = path + "/data666.plist"
  38. //###注意加横杠“/”,否则真机测试崩溃,无法写入####
  39. if FileManager.default.fileExists(atPath: filePath) == false {
  40. let rootDict = NSMutableDictionary()
  41. rootDict.write(toFile: filePath, atomically: true)
  42. print("plist创建成功")
  43. }else{
  44. print("文件已存在")
  45. }
  46. }
  47. //遍历数据
  48. func findPlistFileData(_ key :String) -> Bool {
  49. if let dictData = NSMutableDictionary(contentsOfFile: filePath) {
  50. for (k,_) in dictData{
  51. //判断是否存在相同蓝牙名称
  52. if key == (k as! String){
  53. return true
  54. }
  55. }
  56. }
  57. return false
  58. }
  59. //保存数据
  60. func savePlistData(_ key:String, deviceData:[String:Any]) {
  61. let rootDict = NSMutableDictionary(contentsOfFile: filePath) // 根
  62. rootDict?.setObject(deviceData, forKey: key as NSCopying)
  63. rootDict?.write(toFile: filePath, atomically: true)
  64. print("保存成功 -- \(filePath)")
  65. }
  66. //保存
  67. func saveData (_ key:String, deviceData:[String:Any]) {
  68. guard findPlistFileData(key) == false else {
  69. //设备已存在
  70. // self.savePlistData()
  71. print("该设备已存在")
  72. return
  73. }
  74. self.savePlistData(key, deviceData: deviceData)
  75. }
  76. func deletePlistData(_ key:String) {
  77. let rootDict = NSMutableDictionary(contentsOfFile: filePath) //根
  78. rootDict?.removeObject(forKey: key as Any) // 二级
  79. rootDict?.write(toFile: filePath, atomically: true)
  80. print("删除成功")
  81. }
  82. func deleteData(_ key:String) {
  83. guard findPlistFileData(key) == true else {
  84. print("该设备不存在")
  85. return
  86. }
  87. self.deletePlistData(key)
  88. }
  89. //查找
  90. func findData(_ key:String) -> NSDictionary?{
  91. guard findPlistFileData(key) == true else {
  92. print("该学号不存在")
  93. return nil
  94. }
  95. if let dicData = NSMutableDictionary(contentsOfFile: filePath) {
  96. for (k,v) in dicData {
  97. if key == (k as! String) {
  98. let dic2 = v as! NSDictionary
  99. print("该设备存在")
  100. for (k1, v2) in dic2 {
  101. print("\(k1): \(v2)")
  102. }
  103. return dic2
  104. }
  105. }
  106. }
  107. return nil
  108. }
  109. }