123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- //
- // CLPlistTool.swift
- // SolarBT
- //
- // Created by weclouds on 2019/5/30.
- // Copyright © 2019 weclouds. All rights reserved.
- //
- import UIKit
- /*
- 保存数据
- BluetoothName:""
- struct BLEData {
- var device_name :String?
- var device_type :String?
- var device_model:String?
- var online: Bool? // 是否在线
- }
-
-
- 1、 搜索蓝牙设备 (蓝牙名称)
- 2、 连接蓝牙设备
- 3、 读取蓝牙设备部分信息 (型号,类型,名称 )
- 保存到plist
- */
- class CLPlistTool: NSObject {
-
- let BluetoothNameKey = "BluetoothName"
- let DeviceNameKey = "DeviceName"
- let DeviceTypeKey = "DeviceType"
- let DeviceModeKey = "DeviceMode"
-
- var filePath = "" // 文件路径
-
- static let sharedTool : CLPlistTool = {
- let tool = CLPlistTool()
-
- return tool
- }()
-
-
- //判断文件是否存在改plist ,不存在创建
- func createPlistFile() {
- //获取路径
- let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
- filePath = path + "/data666.plist"
- //###注意加横杠“/”,否则真机测试崩溃,无法写入####
- if FileManager.default.fileExists(atPath: filePath) == false {
- let rootDict = NSMutableDictionary()
- rootDict.write(toFile: filePath, atomically: true)
- print("plist创建成功")
-
- }else{
- print("文件已存在")
- }
- }
-
- //遍历数据
- func findPlistFileData(_ key :String) -> Bool {
- if let dictData = NSMutableDictionary(contentsOfFile: filePath) {
- for (k,_) in dictData{
- //判断是否存在相同蓝牙名称
- if key == (k as! String){
- return true
- }
- }
- }
- return false
- }
-
- //保存数据
- func savePlistData(_ key:String, deviceData:[String:Any]) {
- let rootDict = NSMutableDictionary(contentsOfFile: filePath) // 根
- rootDict?.setObject(deviceData, forKey: key as NSCopying)
- rootDict?.write(toFile: filePath, atomically: true)
- print("保存成功 -- \(filePath)")
- }
-
- //保存
- func saveData (_ key:String, deviceData:[String:Any]) {
- guard findPlistFileData(key) == false else {
- //设备已存在
- // self.savePlistData()
- print("该设备已存在")
- return
- }
- self.savePlistData(key, deviceData: deviceData)
- }
-
-
- func deletePlistData(_ key:String) {
- let rootDict = NSMutableDictionary(contentsOfFile: filePath) //根
- rootDict?.removeObject(forKey: key as Any) // 二级
- rootDict?.write(toFile: filePath, atomically: true)
- print("删除成功")
- }
-
-
- func deleteData(_ key:String) {
- guard findPlistFileData(key) == true else {
- print("该设备不存在")
- return
- }
- self.deletePlistData(key)
- }
-
- //查找
- func findData(_ key:String) -> NSDictionary?{
- guard findPlistFileData(key) == true else {
- print("该学号不存在")
- return nil
- }
- if let dicData = NSMutableDictionary(contentsOfFile: filePath) {
- for (k,v) in dicData {
- if key == (k as! String) {
- let dic2 = v as! NSDictionary
- print("该设备存在")
- for (k1, v2) in dic2 {
- print("\(k1): \(v2)")
- }
- return dic2
- }
- }
- }
- return nil
- }
-
- }
|