如何通过nsuserdefaults在swift中存储CBPeripheral等自定义数据?

时间:2016-08-24 11:24:42

标签: ios swift nsuserdefaults nscoding

我在Swift开发。我想通过i=10 sed ''"$i"' d' file 存储自定义数据。

我的自定义数据如下所示

ConnectedDevice.swift

nsuserdefaults

ViewController.swift

import UIKit
import Foundation
import CoreBluetooth
import CoreLocation

class ConnectedDevice : NSObject , NSCoding{

    var RSSI_threshold:NSNumber=0

    var Current_RSSI:NSNumber=0

    var name:String?

    var bdAddr:NSUUID?

    var ConnectState:Bool=false

    var AlertState:Int=0

    var BLEPeripheral : CBPeripheral!

    var DisconnectAddress:[String] = [String]()

    var DisconnectTime:[String] = [String]()

    var Battery:NSInteger=0

    var Location:[CLLocation] = [CLLocation]()

    var AlertStatus:Int!



    func encodeWithCoder(aCoder: NSCoder) {

        aCoder.encodeObject(RSSI_threshold, forKey: "RSSI_threshold")
        aCoder.encodeObject(Current_RSSI, forKey: "Current_RSSI")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(bdAddr, forKey: "bdAddr")
        aCoder.encodeBool(ConnectState, forKey: "ConnectState")
        aCoder.encodeInteger(AlertState, forKey: "AlertState")
        aCoder.encodeObject(BLEPeripheral, forKey: "BLEPeripheral")
        aCoder.encodeObject(DisconnectAddress, forKey: "DisconnectAddress")
        aCoder.encodeObject(DisconnectTime, forKey: "DisconnectTime")
        aCoder.encodeObject(Battery, forKey: "Battery")
        aCoder.encodeObject(Location, forKey: "Location")
        aCoder.encodeObject(AlertStatus, forKey: "AlertStatus")


    }

    override init() {


    }

    required init?(coder aDecoder: NSCoder) {

        self.RSSI_threshold = aDecoder.decodeObjectForKey("RSSI_threshold") as! NSNumber
        self.Current_RSSI = aDecoder.decodeObjectForKey("Current_RSSI") as! NSNumber
        self.name = aDecoder.decodeObjectForKey("name") as? String
        self.bdAddr = aDecoder.decodeObjectForKey("bdAddr") as? NSUUID
        self.ConnectState = aDecoder.decodeObjectForKey("ConnectState") as! Bool
        self.AlertState = aDecoder.decodeObjectForKey("AlertState") as! Int
        self.BLEPeripheral = aDecoder.decodeObjectForKey("BLEPeripheral") as! CBPeripheral
        self.DisconnectAddress = aDecoder.decodeObjectForKey("DisconnectAddress") as! [String]
        self.DisconnectTime = aDecoder.decodeObjectForKey("DisconnectTime") as! [String]
        self.Battery = aDecoder.decodeObjectForKey("Battery") as! NSInteger
        self.Location = aDecoder.decodeObjectForKey("Location") as! [CLLocation]
        self.AlertStatus = aDecoder.decodeObjectForKey("AlertStatus") as! Int
    }
}

当我尝试存储数据时,我使用以下代码:

var userDefault = NSUserDefaults.standardUserDefaults()
var BLEConnectedDevice:[ConnectedDevice] = [ConnectedDevice]()

当我尝试加载数据时,我使用以下代码:

let data = NSKeyedArchiver.archivedDataWithRootObject(BLEConnectedDevice)
        NSUserDefaults.standardUserDefaults().setObject(data, forKey: "BLEConnectedDevice")
       userDefault.synchronize()

但它会在if let Data = NSUserDefaults.standardUserDefaults().objectForKey("BLEConnectedDevice") as? NSData { if let devcie = NSKeyedUnarchiver.unarchiveObjectWithData(Data) as? ConnectedDevice { print("NSUserDefaults = \(devcie.name)") } } 显示错误,错误日志为

aCoder.encodeObject(BLEPeripheral, forKey: "BLEPeripheral")

如何编码2016-08-24 18:37:57.022 Anti-Lost[476:222607] -[CBPeripheral encodeWithCoder:]: unrecognized selector sent to instance 0x14e9fc60 2016-08-24 18:37:57.024 Anti-Lost[476:222607] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CBPeripheral encodeWithCoder:]: unrecognized selector sent to instance 0x14e9fc60'

我错过了什么吗? 如何通过CBPeripheral在swift中存储自定义数据?

1 个答案:

答案 0 :(得分:3)

这是因为CBPeripheral没有实现NSCoding。因此,您无法将BLEPeripheral对象存档到NSUserDefaults

我认为最好的解决方案是直接存储您感兴趣的属性(例如nameidentifier):

aCoder.encodeObject(BLEPeripheral.name, forKey: "BLEPeripheralName")
aCoder.encodeObject(BLEPeripheral.identifier, forKey: "BLEPeripheralIdentifier")

修改:

我认为无论如何,您都无法再次存储和构建CBPeripheral。 Apple提供CBPeripheral这样的对象:

所以,你不能存储这个对象并在以后构建它似乎是非常合乎逻辑的...我认为最好的解决方案是存储BLEPeripheral.identifier并稍后使用{获取CBPeripheral {1}}。您可以使用CBCentralManager方法执行此操作。 Here is the documentation。而且我认为你永远不应该自己构建一个retrievePeripherals(withIdentifiers:)对象。