sunshinejr / SwiftyUserDefaults

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

Access level modifications #187

Closed fredpi closed 5 years ago

fredpi commented 5 years ago

Closes #186.

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...

sunshinejr commented 5 years ago

Thanks for the PR @fredpi! Gonna review it this week, sorry for the delay.