Convert Swift Encodable class typed as Any to dictionary

后端 未结 2 417
暗喜
暗喜 2021-01-22 10:17

In connection with my previous questions, I decided to subclass NSArrayController in order to achieve the desired behavior.

class NSPresetArrayContr         


        
相关标签:
2条回答
  • 2021-01-22 10:50

    The problem is that protocols don't always conform to themselves. PropertyListEncoder's encode(_:) method expects a Value : Encodable argument:

    func encode<Value : Encodable>(_ value: Value) throws -> Data
    

    However the Encodable type itself is currently unable to satisfy this constraint (but it might well do in a future version of the language).

    As explored in the linked Q&A (and also here), one way to work around this limitation is to open the Encodable value in order to dig out the underlying concrete type, which we can substitute for Value. We can do this with a protocol extension, and use a wrapper type in order to encapsulate it:

    extension Encodable {
      fileprivate func openedEncode(to container: inout SingleValueEncodingContainer) throws {
        try container.encode(self)
      }
    }
    
    struct AnyEncodable : Encodable {
      var value: Encodable
      init(_ value: Encodable) {
        self.value = value
      }
      func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        try value.openedEncode(to: &container)
      }
    }
    

    Applied to your example:

    class NSPresetArrayController : NSArrayController {
      override func addObject(_ object: Any) {
        guard let object = object as? Encodable else { 
          // Not encodable, maybe do some error handling.
          return 
        }
        do {
          let encoded = try PropertyListEncoder().encode(AnyEncodable(object))
          let cocoaPropertyList = try PropertyListSerialization.propertyList(from: encoded, format: nil)
          super.addObject(cocoaPropertyList)
        } catch {
          // Couldn't encode. Do some error handling.
        }
      }
    }
    
    0 讨论(0)
  • 2021-01-22 11:03

    The type of the value that you pass to encode(_:) has to be a concrete type that implements Encodable. This means you need to recover the object's real type from the Any that you have. In order to cast, you must have a statically-specified type to which you are casting. You can't say object as! type(of: object), in other words; you have to say object as? MyClass (or in a generic context you can say object as? T).

    Therefore, I believe that the only way to get around this is to statically enumerate the types you are working with, like so:

    import Foundation
    
    struct S : Encodable {
        let i: Int
    }
    
    struct T : Encodable {
        let f: Float
    }
    
    struct U : Encodable {
        let b: Bool
    }
    
    func plistObject(from encodable: Any) -> Any? {
        let encoded: Data?
        switch encodable {
            case let s as S:
                encoded = try? PropertyListEncoder().encode(s)
            case let t as T:
                encoded = try? PropertyListEncoder().encode(t)
            case let u as U:
                encoded = try? PropertyListEncoder().encode(u)
            default:
                encoded = nil
        }
    
        guard let data = encoded else { return nil }
    
        return try? PropertyListSerialization.propertyList(from: data,
                                                           options: [],
                                                           format: nil)
    }
    

    Needless to say, this is rather gross. It's inflexible, repetitive boilerplate. I'm not sure I can actually recommend its use. It's an answer to the literal question, not necessarily a solution to the problem.

    0 讨论(0)
提交回复
热议问题