- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
@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!)
unArchive
变量)设置的断点时,会发生 fatal error 。这是控制台的消息。
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
以便您查看是否发生错误。
关于iOS - 取消归档具有日期类型属性的 NSSecureCoding 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60253397/
我正在了解 NSSecureCoding Apple 在 iOS 6 中引入的协议(protocol)。 据我目前的理解,只要一个类对其自身的实例进行编码/解码,就应该使用它,以防止替换攻击。 我想知
@objc(EventNotificationInfo) class EventNotificationInfo: NSObject, NSSecureCoding { pub
从 AppDelegate 我称之为: -(void)application:(UIApplication *)application handleWatchKitExtensionRequest:(
我知道使用“NSSecureCoding”可以将数据安全地存储到磁盘,类似于钥匙串(keychain)作为信息安全存储的地方。 我还了解钥匙串(keychain)具有在同一提供商的应用程序之间共享的优
我决定使用 NSSecureCoding 而不是 NSCoding,但我无法让它正常工作。 我预计以下代码会失败,因为我正在编码 NSString 但试图解码 NSNumber。然而,该对象在没有抛出
我有一个类 SGBContainer 的对象它有一个名为 objects 的数组包含类 SGBObject 的对象.目前,它们都实现了 NSCoding 但没有实现 NSSecureCoding。 -
在关注 Creating XPC Services guide 时在 Swift 中并尝试传递自定义 Foo 类,我发现为了使其正常工作,它必须位于动态库中。当它嵌入到两个目标时,与服务的连接失败并返
我在采用 NSSecureCoding 时遇到了问题。我对一个包含自定义类对象的数组进行编码,该类正确地采用了 NSSecureCoding。当我解码它时,传递类 NSArray(这是我编码的对象的类
我正在尝试从 .scn(最初是我转换为 .scn 的 DAE 文件)创建一个 SCNNode,当我这样做时,控制台会生成以下内容: [general] NSSecureCoding allowed c
我正在尝试将一段 Objective-C 代码转换为 Swift 代码。 objective-C : #import "SUCacheItem.h" #define SUCACHEITEM_TOKEN
我有一个变量 var pausedTime: TimeInterval? 我想使用 NSSecureCoding 对其进行编码和解码 到目前为止我有这个用于编码: aCoder.encode(paus
我整个下午都在用头撞墙试图弄清楚为什么这个类的解码失败。该类有一个属性是 Foo 对象的 NSArray。 Foo 符合 NSSecureCoding,我已经成功地自行编码和解码了该类。我在 init
我目前正在试用 iOS 10 Beta 并决定将我的 Swift 代码转换为 Swift 3。直到现在,我能够使用以下方法安全地解码字符串 coder.decodeObjectOfClass(NSSt
背景 我们有多个符合 NSSecureCoding 协议(protocol)的类。 @interface ClassA : NSObject // ... @end @interface Class
我有一些代码可以创建一个名为“content”的 NSSecureCoding 变量,我想将该变量转换为 NSData,然后可以将其制作成 UIImage 或发送到本地服务器。我该如何正确转换?我想要
我正在创建一个链接列表并使用容器对对象、下一个和上一个属性进行分组。与 Foundation 集合一样,我希望它能够实现 NSSecureCoding。这是声明: @interface ListCon
我正在尝试为我的应用制作一个 Action 扩展,用户可以在其中添加他的当前位置以及一些其他数据。在与 Apple Maps App 共享位置后,我调试了扩展程序,发现 Maps 发送了四个提供程序,
我正在使用 loadItemForTypeIdentifier:options:completionHandler: NSItemProvider 对象上的方法,用于通过 iOS 8 中的共享扩展从
根据Apple规范,当一个类支持NSSecureCoding时,它应该使用 decodeObjectOfClass:forKey: 在 initWithCoder 但是由于字典不知道其内容,它如何支持
我正在尝试对 SceneKit SCNMatrix4 进行安全编码,使其符合 NSSecureCoding,类似于以下内容: if let transform = aDecoder.decodeObj
我是一名优秀的程序员,十分优秀!