Support types that conform to both `Codable` and `NSSecureCoding` (#80)

This commit is contained in:
Sindre Sorhus 2021-09-11 10:49:06 +07:00 committed by GitHub
parent e27b3666d2
commit 04f9b3f45e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 0 deletions

View File

@ -46,6 +46,13 @@ extension Defaults {
public struct RawRepresentableCodableBridge<Value: RawRepresentable & Codable>: CodableBridge {}
}
/**
This exists to avoid compiler ambiguity.
*/
extension Defaults {
public struct CodableNSSecureCodingBridge<Value: Codable & NSSecureCoding>: CodableBridge {}
}
extension Defaults {
public struct URLBridge: CodableBridge {
public typealias Value = URL

View File

@ -86,6 +86,10 @@ extension Defaults.Serializable where Self: Codable {
public static var bridge: Defaults.TopLevelCodableBridge<Self> { Defaults.TopLevelCodableBridge() }
}
extension Defaults.Serializable where Self: Codable & NSSecureCoding {
public static var bridge: Defaults.CodableNSSecureCodingBridge<Self> { Defaults.CodableNSSecureCodingBridge() }
}
extension Defaults.Serializable where Self: RawRepresentable {
public static var bridge: Defaults.RawRepresentableBridge<Self> { Defaults.RawRepresentableBridge() }
}

View File

@ -8,6 +8,19 @@ private struct Unicorn: Codable, Defaults.Serializable {
private let fixtureCodable = Unicorn(isUnicorn: true)
@objc(UnicornCodableAndNSSecureCoding)
private final class UnicornCodableAndNSSecureCoding: NSObject, NSSecureCoding, Codable, Defaults.Serializable {
static let supportsSecureCoding = true
func encode(with coder: NSCoder) {}
init?(coder: NSCoder) {}
override init() {
super.init()
}
}
extension Defaults.Keys {
fileprivate static let codable = Key<Unicorn>("codable", default: fixtureCodable)
fileprivate static let codableArray = Key<[Unicorn]>("codable", default: [fixtureCodable])
@ -120,6 +133,11 @@ final class DefaultsCodableTests: XCTestCase {
XCTAssertFalse(Defaults[.codableDictionary]["0"]?.isUnicorn ?? true)
}
func testCodableAndNSSecureCoding() {
let fixture = UnicornCodableAndNSSecureCoding()
_ = Defaults.Key<UnicornCodableAndNSSecureCoding>("testCodableAndNSSecureCoding", default: fixture)
}
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, iOSApplicationExtension 13.0, macOSApplicationExtension 10.15, tvOSApplicationExtension 13.0, watchOSApplicationExtension 6.0, *)
func testObserveKeyCombine() {
let key = Defaults.Key<Unicorn>("observeCodableKeyCombine", default: fixtureCodable)

View File

@ -80,6 +80,7 @@ Add `https://github.com/sindresorhus/Defaults` in the [“Swift Package Manager
- `NSColor` (macOS)
- `UIColor` (iOS)
- `Codable`
- `NSSecureCoding`
Defaults also support the above types wrapped in `Array`, `Set`, `Dictionary`, and even wrapped in nested types. For example, `[[String: Set<[String: Int]>]]`.
@ -87,6 +88,8 @@ For more types, see the [enum example](#enum-example), [`Codable` example](#coda
You can easily add support for any custom type.
If a type conforms to both `NSSecureCoding` and `Codable`, then `Codable` will be used for the serialization.
## Usage
You declare the defaults keys upfront with type and default value.