NSKeyedUnarchiver.unarchiveObject 不工作 [重复]
Posted
技术标签:
【中文标题】NSKeyedUnarchiver.unarchiveObject 不工作 [重复]【英文标题】:NSKeyedUnarchiver.unarchiveObject not working [duplicate] 【发布时间】:2017-03-05 12:29:28 【问题描述】:NSKeyedArchiver.archiveRootObject 被调用,但 getter 将变量 'a' 返回为 nil。这让我很烦。
有人知道这里有什么问题吗?
let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("Information.ab")
var info: Information?
get
let a = NSKeyedUnarchiver.unarchiveObject(withFile: url.absoluteString) as? Information
return a
set
if info != newValue && newValue != nil
NSKeyedArchiver.archiveRootObject(newValue as Any, toFile: url.absoluteString)
class Information: RecordModel
let rules: CKAsset
let aboutUs: String
let terms: String
let privacyPolicy: String
override init(record: CKRecord)
rules = record["rules"] as! CKAsset
aboutUs = record["aboutUs"] as! String
terms = record["terms"] as! String
privacyPolicy = record["privacyPolicy"] as! String
super.init(record: record)
class RecordModel: NSObject, NSCoding
let record: CKRecord
init(record: CKRecord)
self.record = record
required convenience init?(coder aDecoder: NSCoder)
let record = aDecoder.decodeObject(forKey: "keyRecord") as! CKRecord
self.init(record: record)
func encode(with aCoder: NSCoder)
aCoder.encode(record, forKey: "keyRecord")
override func isEqual(_ object: Any?) -> Bool
if let rhs = object as? Information
return record.recordID == rhs.record.recordID
return false
override var hash: Int
return record.recordID.hashValue
【问题讨论】:
【参考方案1】:unarchiveObject(withFile
和 archiveRootObject(_:toFile
都期望文件 path 没有 file://
方案,所以在这两种情况下都是
url.path
而不是url.absoluteString
【讨论】:
以上是关于NSKeyedUnarchiver.unarchiveObject 不工作 [重复]的主要内容,如果未能解决你的问题,请参考以下文章