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

时间:2016-08-24 09:34:15

标签: ios swift nsuserdefaults

我在Swift开发。我想通过nsuserdefaults存储自定义数据。

我的自定义数据如下所示

ConnectedDevice.swift

import UIKit
import Foundation


class ConnectedDevice {

    var name:String?

    var ConnectState:Bool=false

    var AlertState:Int=0

    var Battery:NSInteger=0

    var AlertStatus:Int!

}

ViewController.swift

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

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

userDefault.setValue(ConnDevice, forKey: "ConnectedDevice")
userDefault.synchronize()

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

var test = userDefault.objectForKey("ConnectedDevice")
if((test) != nil){
    ConnDevice = test as! [ConnectedDevice]
}

但是当我尝试通过nsuserdefaults存储数据时,它会显示错误日志,如下所示。似乎数据类型无效。

2016-08-24 17:17:50.315 Anti-Lost[471:212884] Attempt to set a non-property-list object (
    "Anti_Lost.ConnectedDevice"
) as an NSUserDefaults/CFPreferences value for key ConnectedDevice
2016-08-24 17:17:50.318 Anti-Lost[471:212884] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object (
    "Anti_Lost.ConnectedDevice"
) for key ConnectedDevice'

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

我修改代码如下:

ConnectedDevice.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
    }
}

我使用以下代码存储数据

let data = NSKeyedArchiver.archivedDataWithRootObject(ConnectedDevice)
NSUserDefaults.standardUserDefaults().setObject(data, forKey: "ConnectedDevice")

但它会在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

2 个答案:

答案 0 :(得分:2)

您的课程必须符合NSCoding协议,以下是我的应用程序中的一个小样本:

class BarData: NSObject, NSCoding {
    let url: String
    let date: NSDate
    var isFavorite: Bool = false

    init(url: String, date: NSDate) {
        self.url = url
        self.date = date
    }

    //MARK: - NSCoding -
    @objc required init(coder aDecoder: NSCoder) {
        url = aDecoder.decodeObjectForKey("url") as! String
        date = aDecoder.decodeObjectForKey("date") as! NSDate
        isFavorite = aDecoder.decodeBoolForKey("isFavorite")
    }

    @objc func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(url, forKey: "url")
        aCoder.encodeObject(date, forKey: "date")
        aCoder.encodeBool(isFavorite, forKey: "isFavorite")
    }
}

答案 1 :(得分:1)

尝试userDefault.setObject(ConnDevice, forKey: "ConnectedDevice")而不是userDefault.setValue(ConnDevice, forKey: "ConnectedDevice")