2017-12-31 72 views
0

코어 데이터를 사용할 예정 이었지만 더 쉽다고 생각했지만 클래스의 배열을 저장하는 방법에 대해서는 온라인으로 찾을 수 없었습니다. 클래스 자체에 변수가 있습니다.사용자 정의 클래스의 변수가있는 UserDefaults를 사용하여 사용자 정의 클래스 객체를 인코딩하는 방법은 무엇입니까?

+0

그것을 알아 냈

class Cell: NSObject, NSCoding { var name:String! var tag:String? var more:String? var amount:String! var balance:String! var subCells:[Cell]?//THIS IS THE ONE I'M STUGGLING WITH override init() { super.init() } init(name: String, tag: String, more: String, amount: String, balance: String, subCells: [Cell] = [Cell]()) { super.init() self.name = name self.tag = tag self.more = more self.amount = amount self.balance = balance self.subCells = subCells//THIS } required init(coder decoder: NSCoder) { self.name = decoder.decodeObject(forKey: "name") as! String self.tag = decoder.decodeObject(forKey: "tag") as? String self.more = decoder.decodeObject(forKey: "more") as? String self.amount = decoder.decodeObject(forKey: "amount") as! String self.balance = decoder.decodeObject(forKey: "balance") as! String self.subCells = decoder.decodeObject(forKey: "subCells") as? [Cell]//THIS } func initWithCoder(decoder: NSCoder) -> Cell{ self.name = decoder.decodeObject(forKey: "name") as! String self.tag = decoder.decodeObject(forKey: "tag") as? String self.more = decoder.decodeObject(forKey: "more") as? String self.amount = decoder.decodeObject(forKey: "amount") as! String self.balance = decoder.decodeObject(forKey: "balance") as! String self.subCells = decoder.decodeObject(forKey: "subCells") as? [Cell]//THIS return self } func encode(with aCoder: NSCoder) { aCoder.encode(self.name, forKey: "name") aCoder.encode(self.tag, forKey: "tag") aCoder.encode(self.more, forKey: "more") aCoder.encode(self.amount, forKey: "amount") aCoder.encode(self.balance, forKey: "balance") aCoder.encode(self.subCells, forKey: "subCell")//THIS } } 

오전 나는이 문제를 해결하기 위해 핵심 데이터를 사용하도록 강요하거나 내가있는 viewDidLoad()에서 전체 클래스를 인코딩하기 전에 배열을 인코딩 할 수 있습니다 - 'init (coder :) '와 같은'initWithCoder (decoder :)'메소드는 필요 없다. – rmaddy

+1

현재 코드에 어떤 문제점이나 오류 메시지가 있습니까? – Paulw11

+0

NS 오류, 다시 인코딩하고 디코딩하여 해결했습니다. 다소 재귀 적입니다. – Rob

답변

0

확인 내가 참고

required init(coder decoder: NSCoder) { 
    self.name = decoder.decodeObject(forKey: "name") as! String 
    self.tag = decoder.decodeObject(forKey: "tag") as? String 
    self.more = decoder.decodeObject(forKey: "more") as? String 
    self.amount = decoder.decodeObject(forKey: "amount") as! String 
    self.balance = decoder.decodeObject(forKey: "balance") as! String 
    self.subCells = NSKeyedUnarchiver.unarchiveObject(with: (decoder.decodeObject(forKey: "subCells") as! NSData) as Data) as? [Cell] 
} 


func encode(with aCoder: NSCoder) { 

    aCoder.encode(self.name, forKey: "name") 
    aCoder.encode(self.tag, forKey: "tag") 
    aCoder.encode(self.more, forKey: "more") 
    aCoder.encode(self.amount, forKey: "amount") 
    aCoder.encode(self.balance, forKey: "balance") 
    aCoder.encode(NSKeyedArchiver.archivedData(withRootObject: self.subCells), forKey: "subCells") 

}