Swift Object Encoder/Decoder compatible with UserDefaults
import ObjectCoder
// Encode
let frame: CGRect = CGRect(x: 10.0, y: 20.0, width: 30.0, height: 40.0)
let encoded: Any = try ObjectEncoder().encode(frame)
let rawValue: [[Double]] = try XCTUnwrap(encoded as? [[Double]])
XCTAssertEqual(rawValue, [[10.0, 20.0], [30.0, 40.0]])
// Set to and get from UserDefaults
let defaults = UserDefaults.standard
defaults.set(encoded, forKey: "frame")
let object: Any = try XCTUnwrap(defaults.object(forKey: "frame"))
// Decode
let decodedFromEncoded: CGRect = try ObjectDecoder().decode(CGRect.self, from: encoded)
let decodedFromDefaults: CGRect = try ObjectDecoder().decode(CGRect.self, from: object)
XCTAssertEqual(decodedFromEncoded, frame)
XCTAssertEqual(decodedFromDefaults, frame)
.package(url: "https://github.com/nearfri/ObjectCoder", from: "1.0.0")
Preferences is released under the MIT license. See LICENSE for more information.