Wun
Wun

Reputation: 6381

How to store the custom data via nsuserdefaults in swift?

I am developing in Swift. I want to store the custom data via nsuserdefaults.

My custom data is like the following

In 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!

}

In ViewController.swift

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

When I try to store the data , I use the following code:

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

When I try to load the data , I use the following code:

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

But it show the error log like the following when I try to store the data via nsuserdefaults. It seems the data type is invalid.

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'

Did I missing something ? How to store the custom data via nsuserdefaults in swift ? ----------------------------------EDIT---------------------------------

I modify the code like the following:

In 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
    }
}

And I use the following code to store the data

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

But it will show the error at aCoder.encodeObject(BLEPeripheral, forKey: "BLEPeripheral") , and the error log is

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'

How to encode the CBPeripheral ?

Upvotes: 0

Views: 889

Answers (2)

Punit
Punit

Reputation: 1340

try userDefault.setObject(ConnDevice, forKey: "ConnectedDevice") instead of userDefault.setValue(ConnDevice, forKey: "ConnectedDevice")

Upvotes: 1

HaneTV
HaneTV

Reputation: 926

Your class must conform to NSCoding protocol, here is a small sample from one of my app :

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")
    }
}

Upvotes: 2

Related Questions