sunshinejr / SwiftyUserDefaults

Modern Swift API for NSUserDefaults
http://radex.io/swift/nsuserdefaults/static
MIT License
4.84k stars 364 forks source link

How to support existing types with different bridges #287

Open jifang opened 2 years ago

jifang commented 2 years ago

In the README, there is an example to support existing types with different bridges, you can extend it similarly:

extension Data: DefaultsSerializable {
    public static var _defaultsArray: DefaultsArrayBridge<[T]> { DefaultsArrayBridge() }
    public static var _defaults: DefaultsDataBridge { DefaultsDataBridge() }
}

However, saying if I'd like to use a different bridge on Data type. For example,

public struct MyDataBridge: DefaultsBridge {

    public init() {}

    public func save(key: String, value: Data?, userDefaults: UserDefaults) {
        // Do nothing
    }

    public func get(key: String, userDefaults: UserDefaults) -> Data? {
        return nil
    }

    public func deserialize(_ object: Any) -> Data? {
        return nil
    }
}

extension Data: DefaultsSerializable {
    public static var _defaults: MyDataBridge { MyDataBridge() }
}

I will get the compilation warning of Conformance of 'Data' to protocol 'DefaultsSerializable' was already stated in the protocol's module 'SwiftyUserDefaults'. So is it even possible to change the bridge for types that have already been implemented in the module?

Thanks