【问题标题】:iOS - unarchive NSSecureCoding Object that has a property of type DateiOS - 取消归档具有日期类型属性的 NSSecureCoding 对象
【发布时间】:2020-02-16 21:28:26
【问题描述】:
   @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

可能是什么问题?如何取消归档此对象?


信息:我使用了String 类型的startTime。它进展顺利。

【问题讨论】:

    标签: ios nscoding nskeyedarchiver


    【解决方案1】:

    根据documentation for NSSecureCoding

    覆盖init(coder:) 的对象必须解码任何封闭的 使用decodeObjectOfClass:forKey: 方法的对象。例如:

    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
    } 
    

    此外,您应该将其包装在do/catch 中,而不是使用try!,以便查看是否发生错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多