This question already has an answer here:
UIImage(contentsOfFile:) returning nil despite file existing in caches directory [duplicate]
(1个答案)
两年前关闭。
调用了NSKeyedArchiver.archiveRootObject,但getter将变量“a”返回为nil。我很烦。
有人看到这里有什么问题吗?
而不是url.absoluteString
(1个答案)
两年前关闭。
调用了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
}
}
最佳答案
unarchiveObject(withFile
和archiveRootObject(_:toFile
都希望文件路径没有file://
方案,因此在这两种情况下
url.path
而不是url.absoluteString
关于ios - NSKeyedUnarchiver.unarchiveObject无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42608366/
10-13 03:43