0
코어 데이터를 사용할 예정 이었지만 더 쉽다고 생각했지만 셀 클래스의 배열을 저장하는 방법에 대해서는 온라인으로 찾을 수 없었습니다. 셀 클래스 자체에 변수가 있습니다.사용자 정의 클래스의 변수가있는 UserDefaults를 사용하여 사용자 정의 클래스 객체를 인코딩하는 방법은 무엇입니까?
코어 데이터를 사용할 예정 이었지만 더 쉽다고 생각했지만 셀 클래스의 배열을 저장하는 방법에 대해서는 온라인으로 찾을 수 없었습니다. 셀 클래스 자체에 변수가 있습니다.사용자 정의 클래스의 변수가있는 UserDefaults를 사용하여 사용자 정의 클래스 객체를 인코딩하는 방법은 무엇입니까?
확인 내가 참고
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")
}
그것을 알아 냈
오전 나는이 문제를 해결하기 위해 핵심 데이터를 사용하도록 강요하거나 내가있는 viewDidLoad()에서 전체 클래스를 인코딩하기 전에 배열을 인코딩 할 수 있습니다 - 'init (coder :) '와 같은'initWithCoder (decoder :)'메소드는 필요 없다. – rmaddy
현재 코드에 어떤 문제점이나 오류 메시지가 있습니까? – Paulw11
NS 오류, 다시 인코딩하고 디코딩하여 해결했습니다. 다소 재귀 적입니다. – Rob