简体   繁体   English

Set的安全编码<customobject></customobject>

[英]Secure coding of Set<CustomObject>

My app uses core data, which stores instances of a custom class CustomClass .我的应用程序使用核心数据,它存储自定义 class CustomClass的实例。
This class has a number of properties, most of them of standard types, but one property is xxx: Set<CustomObject> .这个 class 有许多属性,其中大部分是标准类型,但一个属性是xxx: Set<CustomObject>
The xcdatamodeld thus specifies (among the others with standard types) an attribute xxx of type Transformable .因此, xcdatamodeld指定(在其他标准类型中)一个Transformable类型的属性xxx xxx is the Set<CustomObject> . xxxSet<CustomObject> Its type is Optional and the Transformer is now NSSecureUnarchiveFromData .它的类型是Optional并且 Transformer 现在是NSSecureUnarchiveFromData
Earlier the Transformer was not specified, and thus decoding was not secure.之前没有指定 Transformer,因此解码不安全。 But Apple now advises to use secure coding, since insecure coding will be deprecated in future.但 Apple 现在建议使用安全编码,因为将来不推荐使用不安全编码。

To enable secure coding, I did the following:为了启用安全编码,我执行了以下操作:
CustomClass now adopts NSSecureCoding instead of NSCoding . CustomClass现在采用NSSecureCoding而不是NSCoding
The following var was added to CustomClass :以下var已添加到CustomClass

public static var supportsSecureCoding: Bool { get { return true } }  

Then I tried to modify public required convenience init?(coder aDecoder: NSCoder) {…} so that attribute xxx is securely decoded.然后我尝试修改public required convenience init?(coder aDecoder: NSCoder) {…}以便安全地解码属性xxx I know that instead of我知道,而不是

let xxx = aDecoder.decodeObject(forKey: „xxx“) as? Set<CustomObject>  

I have to use now decodeObject(of:forKey:) , where of is the type of the object to be decoded, here type Set<CustomObject> .我现在必须使用decodeObject(of:forKey:) ,其中of是要解码的 object 的类型,这里输入Set<CustomObject>
My problem is that I don't know how to formulate this: If I use我的问题是我不知道如何制定这个:如果我使用

let xxx = aDecoder.decodeObject(of: Set<CustomObject>.self, forKey: „xxx“)  

I get the error Cannot convert value of type 'Set<CustomObject>.Type' to expected argument type '[AnyClass]?' (aka 'Optional<Array<AnyObject.Type>>')我收到错误Cannot convert value of type 'Set<CustomObject>.Type' to expected argument type '[AnyClass]?' (aka 'Optional<Array<AnyObject.Type>>') Cannot convert value of type 'Set<CustomObject>.Type' to expected argument type '[AnyClass]?' (aka 'Optional<Array<AnyObject.Type>>') . Cannot convert value of type 'Set<CustomObject>.Type' to expected argument type '[AnyClass]?' (aka 'Optional<Array<AnyObject.Type>>')
Apparently the compiler did not compile显然编译器没有编译

func decodeObject<DecodedObjectType>(of cls: DecodedObjectType.Type, forKey key: String) -> DecodedObjectType? where DecodedObjectType : NSObject, DecodedObjectType : NSCoding  

but instead但反而

func decodeObject(of classes: [AnyClass]?, forKey key: String) -> Any?

ie it treated Set<CustomObject> not as a single type, but as a collection of types.即,它不将Set<CustomObject>视为单一类型,而是将其视为类型的集合。

So, how do I specify that only a single type should be decoded, namely Set<CustomObject> ?那么,如何指定只解码一个类型,即Set<CustomObject>

Unfortunately, I could not find anything in the Apple docs, but I found a hint to the solution in this post :不幸的是,我在 Apple 文档中找不到任何内容,但我在这篇文章中找到了解决方案的提示:
NSSecureCoding is not available for all standard swift classes. NSSecureCoding不适用于所有标准 swift 类。 For those classes, that don't support it, one has to use the Objective-C counterpart classes, ie NSString instead of String .对于那些不支持它的类,必须使用 Objective-C 对应类,即NSString而不是String

One example: If var string = "String" has to be securely encoded, one has to use eg aCoder.encode(string as NSString, forKey: „string“) .一个例子:如果必须对var string = "String"进行安全编码,则必须使用例如aCoder.encode(string as NSString, forKey: „string“)

Now currently Set is not supported by NSSecureCoding .现在NSSecureCoding目前不支持Set I had to use thus我不得不这样使用

let aSet: Set<CustomObject> = []
aCoder.encode(aSet as NSSet, forKey: „aSet“)  

and

let decodedSet = aDecoder.decodeObject(of: NSSet.self, forKey: „aSet“) as? Set<CustomObject>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 收纳套装<CustomObject>到用户默认值 - Storing Set<CustomObject> to UserDefaults 核心数据中的安全编码测量类,NSValueTransformers - Secure Coding Measurement Classes in Core Data, NSValueTransformers 如何确定哪个 Core Data 可转换属性未使用安全编码 - How to determine which Core Data transformable attribute is not using secure coding 遍历字符串字典,customObject 并调用 customObject 初始化程序 - Iterate through dictionary of string, customObject and call customObject initialiser 在Swift中使用未声明的类型&#39;CustomObject&#39;的错误-iOS - The error of Use of undeclared type 'CustomObject' in swift - ios 储存字典 <String, CustomObject> 在Swift中的NSUserDefaults中 - Store a Dictionary<String, CustomObject> in NSUserDefaults in Swift Xcode错误:无法设置()用户定义的检查属性:此类不是密钥的密钥值编码兼容 - Xcode error: Failed to set () user defined inspected property: this class is not key value coding-compliant for the key 未能设置 (key_name) 用户定义的检查属性。 此 class 不符合键值编码 - Failed to set (key_name) user defined inspected property . this class is not key value coding-compliant for the key 订单集合查看基于customObject属性的单元格 - Swift 4 -Xcode 9 - Order Collection View Cells based on customObject Property - Swift 4 -Xcode 9 无法将类型“ [CustomObject]”的值转换为预期的参数类型“ [_]” - Cannot convert value of type '[CustomObject]' to expected argument type '[_]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM