iOS - 取消存档具有日期类型 属性 的 NSSecureCoding 对象
iOS - unarchive NSSecureCoding Object that has a property of type Date
@objc(EventNotificationInfo)
class EventNotificationInfo: NSObject, NSSecureCoding {
public var name: String
public var startTime: Date
public var hexColor: String
init(name: String, startTime: Date, hexColor: String) {
self.name = name
self.startTime = startTime
self.hexColor = hexColor
}
private struct Keys {
static var name: String = "name"
static let startTime: String = "startTime"
static let hexColor: String = "hexColor"
}
static var supportsSecureCoding: Bool = true
func encode(with coder: NSCoder) {
coder.encode(name, forKey: Keys.name)
coder.encode(startTime, forKey: Keys.startTime)
coder.encode(hexColor, forKey: Keys.hexColor)
}
required init?(coder: NSCoder) {
guard let name = coder.decodeObject(forKey: Keys.name) as? String else {
return nil
}
guard let startTime = coder.decodeObject(forKey: Keys.startTime) as? Date else {
print("You are here")
return nil
}
guard let color = coder.decodeObject(forKey: Keys.hexColor) as? String else {
return nil
}
self.name = name
self.startTime = startTime
self.hexColor = color
}
}
EventNotificationInfo
对象的实例。
let event = EventNotificationInfo(name: "Hello from California",
startTime:Date(),
hexColor: "000000")
使用以下代码,我归档和取消归档上述对象。
let eventInfo = try? NSKeyedArchiver.archivedData(withRootObject: event, requiringSecureCoding: false)
let unArchive = try! NSKeyedUnarchiver.unarchivedObject(ofClass: TPEventNotificationInfo.self, from: eventInfo!)
当 xCode 到达我在最后一行设置的断点(unArchive
变量)时发生致命错误。这是控制台的消息。
You are here.
Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=4864 "value for key 'startTime' was of unexpected class 'NSDate'. Allowed classes are '{(
EventNotificationInfo
)}'." UserInfo={NSDebugDescription=value for key 'startTime' was of unexpected class 'NSDate'. Allowed classes are '{(
EventNotificationInfo
)}'.}: file
可能是什么问题?如何取消存档此对象?
信息:我使用了 startTime
类型 String
。一切顺利。
根据 documentation for NSSecureCoding
:
An object that does override init(coder:)
must decode any enclosed
objects using the decodeObjectOfClass:forKey:
method. For example:
let obj = decoder.decodeObject(of:MyClass.self, forKey: "myKey")
您应该在实施 init(coder:)
时这样做。
guard let startTime = coder.decodeObject(of: NSDate.self, forKey: Keys.startTime) as Date? else {
return nil
}
此外,不要使用 try!
,您应该将其包装在 do
/catch
中,以便查看是否发生错误。
@objc(EventNotificationInfo)
class EventNotificationInfo: NSObject, NSSecureCoding {
public var name: String
public var startTime: Date
public var hexColor: String
init(name: String, startTime: Date, hexColor: String) {
self.name = name
self.startTime = startTime
self.hexColor = hexColor
}
private struct Keys {
static var name: String = "name"
static let startTime: String = "startTime"
static let hexColor: String = "hexColor"
}
static var supportsSecureCoding: Bool = true
func encode(with coder: NSCoder) {
coder.encode(name, forKey: Keys.name)
coder.encode(startTime, forKey: Keys.startTime)
coder.encode(hexColor, forKey: Keys.hexColor)
}
required init?(coder: NSCoder) {
guard let name = coder.decodeObject(forKey: Keys.name) as? String else {
return nil
}
guard let startTime = coder.decodeObject(forKey: Keys.startTime) as? Date else {
print("You are here")
return nil
}
guard let color = coder.decodeObject(forKey: Keys.hexColor) as? String else {
return nil
}
self.name = name
self.startTime = startTime
self.hexColor = color
}
}
EventNotificationInfo
对象的实例。
let event = EventNotificationInfo(name: "Hello from California",
startTime:Date(),
hexColor: "000000")
使用以下代码,我归档和取消归档上述对象。
let eventInfo = try? NSKeyedArchiver.archivedData(withRootObject: event, requiringSecureCoding: false)
let unArchive = try! NSKeyedUnarchiver.unarchivedObject(ofClass: TPEventNotificationInfo.self, from: eventInfo!)
当 xCode 到达我在最后一行设置的断点(unArchive
变量)时发生致命错误。这是控制台的消息。
You are here.
Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=4864 "value for key 'startTime' was of unexpected class 'NSDate'. Allowed classes are '{(
EventNotificationInfo
)}'." UserInfo={NSDebugDescription=value for key 'startTime' was of unexpected class 'NSDate'. Allowed classes are '{(
EventNotificationInfo
)}'.}: file
可能是什么问题?如何取消存档此对象?
信息:我使用了 startTime
类型 String
。一切顺利。
根据 documentation for NSSecureCoding
:
An object that does override
init(coder:)
must decode any enclosed objects using thedecodeObjectOfClass:forKey:
method. For example:
let obj = decoder.decodeObject(of:MyClass.self, forKey: "myKey")
您应该在实施 init(coder:)
时这样做。
guard let startTime = coder.decodeObject(of: NSDate.self, forKey: Keys.startTime) as Date? else {
return nil
}
此外,不要使用 try!
,您应该将其包装在 do
/catch
中,以便查看是否发生错误。