[英]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>
. xxx
是Set<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.