You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This project is great! I've been working on something similar for my app but was having trouble with the optionals. Your approach helped me a great deal!
The current code gives Redundant conformance constraint 'Convertor': 'PersistedStorageConvertor' warnings for all of the initializer extensions. In Swift 5.3, you could use Contextual Where Clauses to fix this, and as a side benefit, you only need one extension.
Such as:
extension Persisted {
init(_ key: String, defaultValue: Exposed, storage: UserDefaults = .standard) where Convertor == IdentityStorageConvertor<NonOptionalExposed>, Exposed == NonOptionalExposed {
self.init(key: key, defaultValue: defaultValue, valueConvertor: .init(), storage: storage)
}
init(_ key: String, storage: UserDefaults = .standard) where Convertor == IdentityStorageConvertor<NonOptionalExposed>, Exposed == NonOptionalExposed? {
self.init(key: key, defaultValue: nil, valueConvertor: .init(), storage: storage)
}
init(_ key: String, defaultValue: Exposed, storage: UserDefaults = .standard) where Convertor == RawRepresentableStorageConvertor<NonOptionalExposed>, NonOptionalExposed: RawRepresentable, Exposed == NonOptionalExposed {
self.init(key: key, defaultValue: defaultValue, valueConvertor: RawRepresentableStorageConvertor(), storage: storage)
}
init(_ key: String, storage: UserDefaults = .standard) where Convertor == RawRepresentableStorageConvertor<NonOptionalExposed>, NonOptionalExposed: RawRepresentable, Exposed == NonOptionalExposed? {
self.init(key: key, defaultValue: nil, valueConvertor: RawRepresentableStorageConvertor(), storage: storage)
}
// Note the different parameter name in the following: encodedDataKey vs unnamed. This is reqired since some Codable types
// are also UserDefaultsPrimitive or RawRepresentable. We need a different key to be able to avoid ambiguity.
init(encodedDataKey key: String, defaultValue: Exposed, storage: UserDefaults = .standard) where Convertor == CodableStorageConvertor<NonOptionalExposed>, Exposed == NonOptionalExposed {
self.init(key: key, defaultValue: defaultValue, valueConvertor: CodableStorageConvertor(), storage: storage)
}
init(encodedDataKey key: String, storage: UserDefaults = .standard) where Convertor == CodableStorageConvertor<NonOptionalExposed>, Exposed == NonOptionalExposed? {
self.init(key: key, defaultValue: nil, valueConvertor: CodableStorageConvertor(), storage: storage)
}
}
The text was updated successfully, but these errors were encountered:
Great to hear that the project has been useful! Yes, properly handling the optionals was a bit tricky; it wasn't at all obvious at first (to me) that having two generic arguments (one the unwrapped type) was the solution.
Thanks for raising this - I had noticed that I'd started getting some build warnings when using this in Reading List, but had not yet looked into it. I'd be happy to merge this if you raise it as a pull request; otherwise I can make this change myself soon.
rob-secondstage
added a commit
to rob-secondstage/PersistedPropertyWrapper
that referenced
this issue
Oct 5, 2020
Changed from separate extension for each initializer to a single extension where each initializer uses a 'contextual where clause' to specify its constraints.
This removes the 'Redundant conformance constraint 'Convertor': 'PersistedStorageConvertor' warnings for the separate extensions.
FixesAndrewBennet#1.
This project is great! I've been working on something similar for my app but was having trouble with the optionals. Your approach helped me a great deal!
The current code gives
Redundant conformance constraint 'Convertor': 'PersistedStorageConvertor'
warnings for all of the initializer extensions. In Swift 5.3, you could use Contextual Where Clauses to fix this, and as a side benefit, you only need one extension.Such as:
The text was updated successfully, but these errors were encountered: