A thought regarding the set(encodable:forKey:) interface:
/// Encodes passed `encodable` and saves the resulting data into the user defaults for the key `key`.
/// Any error encoding will result in an assertion failure.
func set<T: Encodable>(encodable: T, forKey key: String) {
do {
let data = try JSONEncoder().encode(encodable)
set(data, forKey: key)
} catch {
assertionFailure("Failure encoding encodable of type \(T.self): \(error.localizedDescription)")
}
}
This PR makes this interface public, without adjusting its error handling behavior. One could argue this is okay, because when using the DefaultsCodableBridge, the same error handling is done (crash on encoding failure).
However, this poses the question, whether the DefaultsCodableBridge should include error handling itself... Maybe make the save function of DefaultsBridge throwing (allowing the user to catch errors if needed), while force-trying on the subscript? (Throwing subscripts are not a thing as far as I know and if they were, they would definitely not look that swifty). Maybe introduce customizable failure fallback strategies as a param on DefaultsCodableBridge initialization?
Maybe, an unexpected failure encoding is practically impossible, if that'd be the case, we should not think too much about it...
Closes #186.
A thought regarding the
set(encodable:forKey:)
interface:This PR makes this interface
public
, without adjusting its error handling behavior. One could argue this is okay, because when using theDefaultsCodableBridge
, the same error handling is done (crash on encoding failure).However, this poses the question, whether the
DefaultsCodableBridge
should include error handling itself... Maybe make thesave
function ofDefaultsBridge
throwing (allowing the user to catch errors if needed), while force-trying on the subscript? (Throwing subscripts are not a thing as far as I know and if they were, they would definitely not look that swifty). Maybe introduce customizable failure fallback strategies as a param onDefaultsCodableBridge
initialization?Maybe, an unexpected failure encoding is practically impossible, if that'd be the case, we should not think too much about it...