]> git.saurik.com Git - apple/security.git/blob - keychain/TrustedPeersHelper/SetValueTransformer.swift
Security-59754.80.3.tar.gz
[apple/security.git] / keychain / TrustedPeersHelper / SetValueTransformer.swift
1 import CoreData
2 import Foundation
3
4 @objc(SetValueTransformer)
5 class SetValueTransformer: ValueTransformer {
6 override class func transformedValueClass() -> AnyClass {
7 return NSData.self
8 }
9
10 override class func allowsReverseTransformation() -> Bool {
11 return true
12 }
13
14 override func transformedValue(_ value: Any?) -> Any? {
15 do {
16 guard let value = value else {
17 return nil
18 }
19 return try NSKeyedArchiver.archivedData(withRootObject: value, requiringSecureCoding: true)
20 } catch {
21 os_log("Failed to serialize a Set: %@", log: tplogDebug, type: .default, error as CVarArg)
22 return nil
23 }
24 }
25
26 override func reverseTransformedValue(_ value: Any?) -> Any? {
27 do {
28 guard let dataOp = value as? Data? else {
29 return nil
30 }
31 guard let data = dataOp else {
32 return nil
33 }
34
35 let unarchiver = try NSKeyedUnarchiver(forReadingFrom: data)
36 return unarchiver.decodeObject(of: [NSSet.self], forKey: NSKeyedArchiveRootObjectKey)
37 } catch {
38 os_log("Failed to deserialize a purported Set: %@", log: tplogDebug, type: .default, error as CVarArg)
39 return nil
40 }
41 }
42
43 static let name = NSValueTransformerName(rawValue: "SetValueTransformer")
44 }