2016-08-24 1 views
1

저는 Swift에서 개발 중입니다. nsuserdefaults을 통해 맞춤 데이터를 저장하고 싶습니다.신속하게 nsuserdefaults를 통해 CBPeripheral과 같은 사용자 정의 데이터를 저장하는 방법은 무엇입니까?

내 사용자 정의 데이터는 내가 데이터를 저장하려고하면이 ViewController.swift

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

에서 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(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?

내가 누락 되었습니까? nsuserdefaults을 통해 맞춤 데이터를 신속하게 저장하는 방법은 무엇입니까?

+0

'CBPeripheral'은'NSCoding'을 따르지 않습니다. 당신은 그것을 저장할 수 없습니다. 당신은 그것에 관하여 "재미있는 정보"만 저장하고 싶을지도 모르지만, 이렇게 저장할 수는 없습니다. – Larme

답변

2

CBPeripheralNSCoding을 구현하지 않기 때문입니다. 따라서 BLEPeripheral 개체를 NSUserDefaults에 보관할 수 없습니다.

나는 가장 좋은 방법은 당신이 (같은 name 또는 identifier)에 관심이있는 직접 속성을 저장하는 것입니다 생각 :

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

편집 : 나는 당신이 저장할 수 없습니다 생각

아무리해도 CBPeripheral을 다시 빌드하십시오. 애플은이 같은 CBPeripheral 개체를 제공합니다 : 그래서

,이 객체를 저장하고 나중에 다시 만들 수없는 매우 논리적 인 것 같다 ... 나는 최선의 해결책은 BLEPeripheral.identifier를 저장하고를 사용하여 CBPeripheral 나중에 다시 가져 오기 위해 생각 CBCentralManager. retrievePeripherals(withIdentifiers:) 방법을 사용하여이 작업을 수행 할 수 있습니다. Here is the documentation. 그리고 나는 너 자신이 CBPeripheral 개체를 결코 만들어야한다고 생각한다.

+0

그리고'self.BLEPeripheral.name = aDecoder.decodeObjectForKey ("BLEPeripheralName")을! String'하지만 '속성에 할당 할 수 없습니다 :'name '은 get-only 속성입니다' – Wun

+0

실제로. NSUserDefaults에서 BLEPeripheral 개체를 다시 빌드 할 수있을 것 같지 않습니다. 그리고 우리가 그것에 대해 생각할 때, 그것은 정상적인 것 같지 않습니까? –

+0

방금'BLEPeripheral'을 저장하고 다시 빌드하면 안되는 이유를 설명하기 위해 답을 편집했습니다. –

관련 문제