Upgrade to Swit 5.7
This commit is contained in:
parent
981ccb0a01
commit
bc1af5d872
|
@ -4,12 +4,12 @@ on:
|
||||||
- pull_request
|
- pull_request
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
runs-on: macos-11
|
runs-on: macos-12
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v3
|
||||||
- run: swift test
|
- run: swift test
|
||||||
lint:
|
lint:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v3
|
||||||
- uses: norio-nomura/action-swiftlint@3.2.1
|
- uses: norio-nomura/action-swiftlint@3.2.1
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
// swift-tools-version:5.5
|
// swift-tools-version:5.7
|
||||||
import PackageDescription
|
import PackageDescription
|
||||||
|
|
||||||
let package = Package(
|
let package = Package(
|
||||||
|
|
|
@ -8,7 +8,7 @@ import UIKit
|
||||||
|
|
||||||
extension Defaults.CodableBridge {
|
extension Defaults.CodableBridge {
|
||||||
public func serialize(_ value: Value?) -> Serializable? {
|
public func serialize(_ value: Value?) -> Serializable? {
|
||||||
guard let value = value else {
|
guard let value else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,11 +25,11 @@ extension Defaults.CodableBridge {
|
||||||
}
|
}
|
||||||
|
|
||||||
public func deserialize(_ object: Serializable?) -> Value? {
|
public func deserialize(_ object: Serializable?) -> Value? {
|
||||||
guard let jsonString = object else {
|
guard let object else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return [Value].init(jsonString: "[\(jsonString)]")?.first
|
return [Value].init(jsonString: "[\(object)]")?.first
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,30 +37,21 @@ extension Defaults.CodableBridge {
|
||||||
Any `Value` that conforms to `Codable` and `Defaults.Serializable` will use `CodableBridge` to do the serialization and deserialization.
|
Any `Value` that conforms to `Codable` and `Defaults.Serializable` will use `CodableBridge` to do the serialization and deserialization.
|
||||||
*/
|
*/
|
||||||
extension Defaults {
|
extension Defaults {
|
||||||
public struct TopLevelCodableBridge<Value: Codable>: CodableBridge {
|
public struct TopLevelCodableBridge<Value: Codable>: CodableBridge {}
|
||||||
// TODO: A temporary workaround for Xcode 13.3 compiler issue. Should remove after https://bugs.swift.org/browse/SR-15807 is fixed.
|
|
||||||
public init() {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
`RawRepresentableCodableBridge` is needed because, for example, with `enum SomeEnum: String, Codable, Defaults.Serializable`, the compiler will be confused between `RawRepresentableBridge` and `TopLevelCodableBridge`.
|
`RawRepresentableCodableBridge` is needed because, for example, with `enum SomeEnum: String, Codable, Defaults.Serializable`, the compiler will be confused between `RawRepresentableBridge` and `TopLevelCodableBridge`.
|
||||||
*/
|
*/
|
||||||
extension Defaults {
|
extension Defaults {
|
||||||
public struct RawRepresentableCodableBridge<Value: RawRepresentable & Codable>: CodableBridge {
|
public struct RawRepresentableCodableBridge<Value: RawRepresentable & Codable>: CodableBridge {}
|
||||||
// TODO: A temporary workaround for Xcode 13.3 compiler issue. Should remove after https://bugs.swift.org/browse/SR-15807 is fixed.
|
|
||||||
public init() {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
This exists to avoid compiler ambiguity.
|
This exists to avoid compiler ambiguity.
|
||||||
*/
|
*/
|
||||||
extension Defaults {
|
extension Defaults {
|
||||||
public struct CodableNSSecureCodingBridge<Value: Codable & NSSecureCoding>: CodableBridge {
|
public struct CodableNSSecureCodingBridge<Value: Codable & NSSecureCoding>: CodableBridge {}
|
||||||
// TODO: A temporary workaround for Xcode 13.3 compiler issue. Should remove after https://bugs.swift.org/browse/SR-15807 is fixed.
|
|
||||||
public init() {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Defaults {
|
extension Defaults {
|
||||||
|
@ -74,19 +65,16 @@ extension Defaults {
|
||||||
public typealias Value = Value
|
public typealias Value = Value
|
||||||
public typealias Serializable = Value.RawValue
|
public typealias Serializable = Value.RawValue
|
||||||
|
|
||||||
// TODO: A temporary workaround for Xcode 13.3 compiler issue. Should remove after https://bugs.swift.org/browse/SR-15807 is fixed.
|
|
||||||
public init() {}
|
|
||||||
|
|
||||||
public func serialize(_ value: Value?) -> Serializable? {
|
public func serialize(_ value: Value?) -> Serializable? {
|
||||||
value?.rawValue
|
value?.rawValue
|
||||||
}
|
}
|
||||||
|
|
||||||
public func deserialize(_ object: Serializable?) -> Value? {
|
public func deserialize(_ object: Serializable?) -> Value? {
|
||||||
guard let rawValue = object else {
|
guard let object else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return Value(rawValue: rawValue)
|
return Value(rawValue: object)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -96,24 +84,21 @@ extension Defaults {
|
||||||
public typealias Value = Value
|
public typealias Value = Value
|
||||||
public typealias Serializable = Data
|
public typealias Serializable = Data
|
||||||
|
|
||||||
// TODO: A temporary workaround for Xcode 13.3 compiler issue. Should remove after https://bugs.swift.org/browse/SR-15807 is fixed.
|
|
||||||
public init() {}
|
|
||||||
|
|
||||||
public func serialize(_ value: Value?) -> Serializable? {
|
public func serialize(_ value: Value?) -> Serializable? {
|
||||||
guard let object = value else {
|
guard let value else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return try? NSKeyedArchiver.archivedData(withRootObject: object, requiringSecureCoding: true)
|
return try? NSKeyedArchiver.archivedData(withRootObject: value, requiringSecureCoding: true)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func deserialize(_ object: Serializable?) -> Value? {
|
public func deserialize(_ object: Serializable?) -> Value? {
|
||||||
guard let data = object else {
|
guard let object else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
return try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? Value
|
return try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(object) as? Value
|
||||||
} catch {
|
} catch {
|
||||||
print(error)
|
print(error)
|
||||||
return nil
|
return nil
|
||||||
|
@ -150,8 +135,8 @@ extension Defaults {
|
||||||
return array.map { Element.bridge.serialize($0) }.compact()
|
return array.map { Element.bridge.serialize($0) }.compact()
|
||||||
}
|
}
|
||||||
|
|
||||||
public func deserialize(_ object: Serializable?) -> Value? {
|
public func deserialize(_ array: Serializable?) -> Value? {
|
||||||
guard let array = object else {
|
guard let array else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,8 +150,8 @@ extension Defaults {
|
||||||
public typealias Value = [Key: Element.Value]
|
public typealias Value = [Key: Element.Value]
|
||||||
public typealias Serializable = [String: Element.Serializable]
|
public typealias Serializable = [String: Element.Serializable]
|
||||||
|
|
||||||
public func serialize(_ value: Value?) -> Serializable? {
|
public func serialize(_ dictionary: Value?) -> Serializable? {
|
||||||
guard let dictionary = value else {
|
guard let dictionary else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,8 +161,8 @@ extension Defaults {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func deserialize(_ object: Serializable?) -> Value? {
|
public func deserialize(_ dictionary: Serializable?) -> Value? {
|
||||||
guard let dictionary = object else {
|
guard let dictionary else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -201,8 +186,8 @@ extension Defaults {
|
||||||
public typealias Value = Set<Element>
|
public typealias Value = Set<Element>
|
||||||
public typealias Serializable = Any
|
public typealias Serializable = Any
|
||||||
|
|
||||||
public func serialize(_ value: Value?) -> Serializable? {
|
public func serialize(_ set: Value?) -> Serializable? {
|
||||||
guard let set = value else {
|
guard let set else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -240,11 +225,8 @@ extension Defaults {
|
||||||
public typealias Element = Value.Element
|
public typealias Element = Value.Element
|
||||||
public typealias Serializable = Any
|
public typealias Serializable = Any
|
||||||
|
|
||||||
// TODO: A temporary workaround for Xcode 13.3 compiler issue. Should remove after https://bugs.swift.org/browse/SR-15807 is fixed.
|
public func serialize(_ setAlgebra: Value?) -> Serializable? {
|
||||||
public init() {}
|
guard let setAlgebra else {
|
||||||
|
|
||||||
public func serialize(_ value: Value?) -> Serializable? {
|
|
||||||
guard let setAlgebra = value else {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -282,11 +264,8 @@ extension Defaults {
|
||||||
public typealias Element = Value.Element
|
public typealias Element = Value.Element
|
||||||
public typealias Serializable = Any
|
public typealias Serializable = Any
|
||||||
|
|
||||||
// TODO: A temporary workaround for Xcode 13.3 compiler issue. Should remove after https://bugs.swift.org/browse/SR-15807 is fixed.
|
public func serialize(_ collection: Value?) -> Serializable? {
|
||||||
public init() {}
|
guard let collection else {
|
||||||
|
|
||||||
public func serialize(_ value: Value?) -> Serializable? {
|
|
||||||
guard let collection = value else {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -328,7 +307,7 @@ extension Defaults {
|
||||||
}
|
}
|
||||||
|
|
||||||
public func deserialize(_ object: Serializable?) -> Value? {
|
public func deserialize(_ object: Serializable?) -> Value? {
|
||||||
guard let object = object else {
|
guard let object else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -344,7 +323,7 @@ extension Defaults {
|
||||||
typealias Bound = T.Bound
|
typealias Bound = T.Bound
|
||||||
|
|
||||||
public func serialize(_ value: Value?) -> Serializable? {
|
public func serialize(_ value: Value?) -> Serializable? {
|
||||||
guard let value = value else {
|
guard let value else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -363,7 +342,7 @@ extension Defaults {
|
||||||
}
|
}
|
||||||
|
|
||||||
public func deserialize(_ object: Serializable?) -> Value? {
|
public func deserialize(_ object: Serializable?) -> Value? {
|
||||||
guard let object = object else {
|
guard let object else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -408,7 +387,7 @@ extension Defaults {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
public func serialize(_ value: Value?) -> Serializable? {
|
public func serialize(_ value: Value?) -> Serializable? {
|
||||||
guard let value = value else {
|
guard let value else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -57,7 +57,7 @@ struct UserBridge: Defaults.Bridge {
|
||||||
typealias Serializable = [String: String]
|
typealias Serializable = [String: String]
|
||||||
|
|
||||||
func serialize(_ value: Value?) -> Serializable? {
|
func serialize(_ value: Value?) -> Serializable? {
|
||||||
guard let value = value else {
|
guard let value else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,7 +69,7 @@ struct UserBridge: Defaults.Bridge {
|
||||||
|
|
||||||
func deserialize(_ object: Serializable?) -> Value? {
|
func deserialize(_ object: Serializable?) -> Value? {
|
||||||
guard
|
guard
|
||||||
let object = object,
|
let object,
|
||||||
let username = object["username"],
|
let username = object["username"],
|
||||||
let password = object["password"]
|
let password = object["password"]
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -198,7 +198,7 @@ extension Defaults.SetAlgebraSerializable where Self: Defaults.NativeType, Eleme
|
||||||
public typealias CodableForm = [Element.CodableForm]
|
public typealias CodableForm = [Element.CodableForm]
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Defaults.CodableType where Self: RawRepresentable, NativeForm: RawRepresentable, RawValue == NativeForm.RawValue {
|
extension Defaults.CodableType where Self: RawRepresentable<NativeForm.RawValue>, NativeForm: RawRepresentable {
|
||||||
public func toNative() -> NativeForm {
|
public func toNative() -> NativeForm {
|
||||||
NativeForm(rawValue: rawValue)!
|
NativeForm(rawValue: rawValue)!
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,8 +11,7 @@ So we can convert the JSON string into a `NativeType` like this:
|
||||||
|
|
||||||
```
|
```
|
||||||
guard
|
guard
|
||||||
let jsonString = string,
|
let jsonData = string?.data(using: .utf8),
|
||||||
let jsonData = jsonString.data(using: .utf8),
|
|
||||||
let codable = try? JSONDecoder().decode(NativeType.CodableForm.self, from: jsonData)
|
let codable = try? JSONDecoder().decode(NativeType.CodableForm.self, from: jsonData)
|
||||||
else {
|
else {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -3,8 +3,7 @@ import Foundation
|
||||||
extension UserDefaults {
|
extension UserDefaults {
|
||||||
func migrateCodableToNative<Value: Defaults.Serializable & Codable>(forKey key: String, of type: Value.Type) {
|
func migrateCodableToNative<Value: Defaults.Serializable & Codable>(forKey key: String, of type: Value.Type) {
|
||||||
guard
|
guard
|
||||||
let jsonString = string(forKey: key),
|
let jsonData = string(forKey: key)?.data(using: .utf8),
|
||||||
let jsonData = jsonString.data(using: .utf8),
|
|
||||||
let codable = try? JSONDecoder().decode(Value.self, from: jsonData)
|
let codable = try? JSONDecoder().decode(Value.self, from: jsonData)
|
||||||
else {
|
else {
|
||||||
return
|
return
|
||||||
|
@ -38,8 +37,7 @@ extension UserDefaults {
|
||||||
*/
|
*/
|
||||||
func migrateCodableToNative<Value: Defaults.NativeType>(forKey key: String, of type: Value.Type) {
|
func migrateCodableToNative<Value: Defaults.NativeType>(forKey key: String, of type: Value.Type) {
|
||||||
guard
|
guard
|
||||||
let jsonString = string(forKey: key),
|
let jsonData = string(forKey: key)?.data(using: .utf8),
|
||||||
let jsonData = jsonString.data(using: .utf8),
|
|
||||||
let codable = try? JSONDecoder().decode(Value.CodableForm.self, from: jsonData)
|
let codable = try? JSONDecoder().decode(Value.CodableForm.self, from: jsonData)
|
||||||
else {
|
else {
|
||||||
return
|
return
|
||||||
|
|
|
@ -58,7 +58,7 @@ extension Defaults {
|
||||||
self.options = options
|
self.options = options
|
||||||
}
|
}
|
||||||
|
|
||||||
func receive<S>(subscriber: S) where S: Subscriber, Failure == S.Failure, Output == S.Input {
|
func receive(subscriber: some Subscriber<Output, Failure>) {
|
||||||
let subscription = DefaultsSubscription(
|
let subscription = DefaultsSubscription(
|
||||||
subscriber: subscriber,
|
subscriber: subscriber,
|
||||||
suite: suite,
|
suite: suite,
|
||||||
|
|
|
@ -43,7 +43,7 @@ extension Defaults {
|
||||||
|
|
||||||
private static func deserialize<Value: Serializable>(_ value: Any?, to type: Value.Type) -> Value? {
|
private static func deserialize<Value: Serializable>(_ value: Any?, to type: Value.Type) -> Value? {
|
||||||
guard
|
guard
|
||||||
let value = value,
|
let value,
|
||||||
!(value is NSNull)
|
!(value is NSNull)
|
||||||
else {
|
else {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -168,7 +168,7 @@ extension Defaults {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
*/
|
*/
|
||||||
public struct Toggle<Label, Key>: View where Label: View, Key: Defaults.Key<Bool> {
|
public struct Toggle<Label: View>: View {
|
||||||
@ViewStorage private var onChange: ((Bool) -> Void)?
|
@ViewStorage private var onChange: ((Bool) -> Void)?
|
||||||
|
|
||||||
private let label: () -> Label
|
private let label: () -> Label
|
||||||
|
@ -176,7 +176,7 @@ extension Defaults {
|
||||||
// Intentionally using `@ObservedObjected` over `@StateObject` so that the key can be dynamically changed.
|
// Intentionally using `@ObservedObjected` over `@StateObject` so that the key can be dynamically changed.
|
||||||
@ObservedObject private var observable: Defaults.Observable<Bool>
|
@ObservedObject private var observable: Defaults.Observable<Bool>
|
||||||
|
|
||||||
public init(key: Key, @ViewBuilder label: @escaping () -> Label) {
|
public init(key: Defaults.Key<Bool>, @ViewBuilder label: @escaping () -> Label) {
|
||||||
self.label = label
|
self.label = label
|
||||||
self.observable = Defaults.Observable(key)
|
self.observable = Defaults.Observable(key)
|
||||||
}
|
}
|
||||||
|
@ -191,8 +191,8 @@ extension Defaults {
|
||||||
}
|
}
|
||||||
|
|
||||||
@available(macOS 11, iOS 14, tvOS 14, watchOS 7, *)
|
@available(macOS 11, iOS 14, tvOS 14, watchOS 7, *)
|
||||||
extension Defaults.Toggle where Label == Text {
|
extension Defaults.Toggle<Text> {
|
||||||
public init<S>(_ title: S, key: Defaults.Key<Bool>) where S: StringProtocol {
|
public init(_ title: some StringProtocol, key: Defaults.Key<Bool>) {
|
||||||
self.label = { Text(title) }
|
self.label = { Text(title) }
|
||||||
self.observable = Defaults.Observable(key)
|
self.observable = Defaults.Observable(key)
|
||||||
}
|
}
|
||||||
|
|
|
@ -104,8 +104,8 @@ final class LifetimeAssociation {
|
||||||
|
|
||||||
private func invalidate() {
|
private func invalidate() {
|
||||||
guard
|
guard
|
||||||
let owner = owner,
|
let owner,
|
||||||
let wrappedObject = wrappedObject,
|
let wrappedObject,
|
||||||
var associatedObjects = Self.associatedObjects[owner],
|
var associatedObjects = Self.associatedObjects[owner],
|
||||||
let wrappedObjectAssociationIndex = associatedObjects.firstIndex(where: { $0 === wrappedObject })
|
let wrappedObjectAssociationIndex = associatedObjects.firstIndex(where: { $0 === wrappedObject })
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -15,7 +15,7 @@ private struct ItemBridge: Defaults.Bridge {
|
||||||
typealias Value = Item
|
typealias Value = Item
|
||||||
typealias Serializable = [String: String]
|
typealias Serializable = [String: String]
|
||||||
func serialize(_ value: Value?) -> Serializable? {
|
func serialize(_ value: Value?) -> Serializable? {
|
||||||
guard let value = value else {
|
guard let value else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ private struct ItemBridge: Defaults.Bridge {
|
||||||
|
|
||||||
func deserialize(_ object: Serializable?) -> Value? {
|
func deserialize(_ object: Serializable?) -> Value? {
|
||||||
guard
|
guard
|
||||||
let object = object,
|
let object,
|
||||||
let name = object["name"],
|
let name = object["name"],
|
||||||
let count = UInt(object["count"] ?? "0")
|
let count = UInt(object["count"] ?? "0")
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -16,7 +16,7 @@ public final class DefaultsUserBridge: Defaults.Bridge {
|
||||||
public typealias Serializable = [String: String]
|
public typealias Serializable = [String: String]
|
||||||
|
|
||||||
public func serialize(_ value: Value?) -> Serializable? {
|
public func serialize(_ value: Value?) -> Serializable? {
|
||||||
guard let value = value else {
|
guard let value else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ public final class DefaultsUserBridge: Defaults.Bridge {
|
||||||
|
|
||||||
public func deserialize(_ object: Serializable?) -> Value? {
|
public func deserialize(_ object: Serializable?) -> Value? {
|
||||||
guard
|
guard
|
||||||
let object = object,
|
let object,
|
||||||
let username = object["username"],
|
let username = object["username"],
|
||||||
let password = object["password"]
|
let password = object["password"]
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -52,7 +52,7 @@ private struct TimeZoneBridge: Defaults.Bridge {
|
||||||
typealias Serializable = [String: Any]
|
typealias Serializable = [String: Any]
|
||||||
|
|
||||||
func serialize(_ value: TimeZone?) -> Serializable? {
|
func serialize(_ value: TimeZone?) -> Serializable? {
|
||||||
guard let value = value else {
|
guard let value else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,9 +61,9 @@ private struct TimeZoneBridge: Defaults.Bridge {
|
||||||
|
|
||||||
func deserialize(_ object: Serializable?) -> TimeZone? {
|
func deserialize(_ object: Serializable?) -> TimeZone? {
|
||||||
guard
|
guard
|
||||||
let dictionary = object,
|
let object,
|
||||||
let id = dictionary["id"] as? String,
|
let id = object["id"] as? String,
|
||||||
let name = dictionary["name"] as? String
|
let name = object["name"] as? String
|
||||||
else {
|
else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -86,7 +86,7 @@ private struct ChosenTimeZoneBridge: Defaults.Bridge {
|
||||||
typealias Serializable = [String: Any]
|
typealias Serializable = [String: Any]
|
||||||
|
|
||||||
func serialize(_ value: Value?) -> Serializable? {
|
func serialize(_ value: Value?) -> Serializable? {
|
||||||
guard let value = value else {
|
guard let value else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,9 +95,9 @@ private struct ChosenTimeZoneBridge: Defaults.Bridge {
|
||||||
|
|
||||||
func deserialize(_ object: Serializable?) -> Value? {
|
func deserialize(_ object: Serializable?) -> Value? {
|
||||||
guard
|
guard
|
||||||
let dictionary = object,
|
let object,
|
||||||
let id = dictionary["id"] as? String,
|
let id = object["id"] as? String,
|
||||||
let name = dictionary["name"] as? String
|
let name = object["name"] as? String
|
||||||
else {
|
else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ extension CustomDate: Defaults.Serializable {
|
||||||
public typealias Serializable = [Int]
|
public typealias Serializable = [Int]
|
||||||
|
|
||||||
public func serialize(_ value: Value?) -> Serializable? {
|
public func serialize(_ value: Value?) -> Serializable? {
|
||||||
guard let value = value else {
|
guard let value else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ extension CustomDate: Defaults.Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
public func deserialize(_ object: Serializable?) -> Value? {
|
public func deserialize(_ object: Serializable?) -> Value? {
|
||||||
guard let object = object else {
|
guard let object else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ private struct ItemBridge: Defaults.Bridge {
|
||||||
typealias Value = Item
|
typealias Value = Item
|
||||||
typealias Serializable = [String: String]
|
typealias Serializable = [String: String]
|
||||||
func serialize(_ value: Value?) -> Serializable? {
|
func serialize(_ value: Value?) -> Serializable? {
|
||||||
guard let value = value else {
|
guard let value else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ private struct ItemBridge: Defaults.Bridge {
|
||||||
|
|
||||||
func deserialize(_ object: Serializable?) -> Value? {
|
func deserialize(_ object: Serializable?) -> Value? {
|
||||||
guard
|
guard
|
||||||
let object = object,
|
let object,
|
||||||
let name = object["name"],
|
let name = object["name"],
|
||||||
let count = UInt(object["count"] ?? "0")
|
let count = UInt(object["count"] ?? "0")
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -7,7 +7,7 @@ struct DefaultsSetAlgebra<Element: Defaults.Serializable & Hashable>: SetAlgebra
|
||||||
|
|
||||||
init() {}
|
init() {}
|
||||||
|
|
||||||
init<S: Sequence>(_ sequence: __owned S) where Element == S.Element {
|
init(_ sequence: __owned some Sequence<Element>) {
|
||||||
self.store = Set(sequence)
|
self.store = Set(sequence)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,38 +0,0 @@
|
||||||
// TODO: A temporary workaround for Xcode 13.3 compiler issue. Should remove after https://bugs.swift.org/browse/SR-15807 is fixed.
|
|
||||||
import Foundation
|
|
||||||
import Defaults
|
|
||||||
|
|
||||||
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: Codable & NSSecureCoding & Defaults.PreferNSSecureCoding {
|
|
||||||
public static var bridge: Defaults.NSSecureCodingBridge<Self> { Defaults.NSSecureCodingBridge() }
|
|
||||||
}
|
|
||||||
|
|
||||||
extension Defaults.Serializable where Self: Codable & RawRepresentable {
|
|
||||||
public static var bridge: Defaults.RawRepresentableCodableBridge<Self> { Defaults.RawRepresentableCodableBridge() }
|
|
||||||
}
|
|
||||||
|
|
||||||
extension Defaults.Serializable where Self: Codable & RawRepresentable & Defaults.PreferRawRepresentable {
|
|
||||||
public static var bridge: Defaults.RawRepresentableBridge<Self> { Defaults.RawRepresentableBridge() }
|
|
||||||
}
|
|
||||||
|
|
||||||
extension Defaults.Serializable where Self: RawRepresentable {
|
|
||||||
public static var bridge: Defaults.RawRepresentableBridge<Self> { Defaults.RawRepresentableBridge() }
|
|
||||||
}
|
|
||||||
extension Defaults.Serializable where Self: NSSecureCoding {
|
|
||||||
public static var bridge: Defaults.NSSecureCodingBridge<Self> { Defaults.NSSecureCodingBridge() }
|
|
||||||
}
|
|
||||||
|
|
||||||
extension Defaults.CollectionSerializable where Element: Defaults.Serializable {
|
|
||||||
public static var bridge: Defaults.CollectionBridge<Self> { Defaults.CollectionBridge() }
|
|
||||||
}
|
|
||||||
|
|
||||||
extension Defaults.SetAlgebraSerializable where Element: Defaults.Serializable & Hashable {
|
|
||||||
public static var bridge: Defaults.SetAlgebraBridge<Self> { Defaults.SetAlgebraBridge() }
|
|
||||||
}
|
|
|
@ -331,7 +331,7 @@ private struct TimeZoneBridge: Defaults.Bridge {
|
||||||
typealias Serializable = [String: String]
|
typealias Serializable = [String: String]
|
||||||
|
|
||||||
func serialize(_ value: TimeZone?) -> Serializable? {
|
func serialize(_ value: TimeZone?) -> Serializable? {
|
||||||
guard let value = value else {
|
guard let value else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -343,9 +343,9 @@ private struct TimeZoneBridge: Defaults.Bridge {
|
||||||
|
|
||||||
func deserialize(_ object: Serializable?) -> TimeZone? {
|
func deserialize(_ object: Serializable?) -> TimeZone? {
|
||||||
guard
|
guard
|
||||||
let dictionary = object,
|
let object,
|
||||||
let id = dictionary["id"],
|
let id = object["id"],
|
||||||
let name = dictionary["name"]
|
let name = object["name"]
|
||||||
else {
|
else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
36
readme.md
36
readme.md
|
@ -31,7 +31,6 @@ For a real-world example, see the [Plash app](https://github.com/sindresorhus/Pl
|
||||||
- Easy to add support for your own custom types.
|
- Easy to add support for your own custom types.
|
||||||
- Comes with a convenience SwiftUI `Toggle` component.
|
- Comes with a convenience SwiftUI `Toggle` component.
|
||||||
|
|
||||||
|
|
||||||
## Compatibility
|
## Compatibility
|
||||||
|
|
||||||
- macOS 10.13+
|
- macOS 10.13+
|
||||||
|
@ -39,35 +38,6 @@ For a real-world example, see the [Plash app](https://github.com/sindresorhus/Pl
|
||||||
- tvOS 12+
|
- tvOS 12+
|
||||||
- watchOS 5+
|
- watchOS 5+
|
||||||
|
|
||||||
<br>
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
<div align="center">
|
|
||||||
<p>
|
|
||||||
<p>
|
|
||||||
<sup>
|
|
||||||
<a href="https://github.com/sponsors/sindresorhus">Sindre's open source work is supported by the community</a>
|
|
||||||
</sup>
|
|
||||||
</p>
|
|
||||||
<sup>Special thanks to:</sup>
|
|
||||||
<br>
|
|
||||||
<br>
|
|
||||||
<a href="https://keygen.sh">
|
|
||||||
<div>
|
|
||||||
<img src="https://sindresorhus.com/assets/thanks/keygen-logo.svg" width="210" alt="Keygen">
|
|
||||||
</div>
|
|
||||||
<b>A dead-simple software licensing and distribution API built for developers</b>
|
|
||||||
</a>
|
|
||||||
<br>
|
|
||||||
<br>
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
<br>
|
|
||||||
|
|
||||||
## Migration Guides
|
## Migration Guides
|
||||||
|
|
||||||
#### [From v4 to v5](./migration.md)
|
#### [From v4 to v5](./migration.md)
|
||||||
|
@ -76,7 +46,7 @@ For a real-world example, see the [Plash app](https://github.com/sindresorhus/Pl
|
||||||
|
|
||||||
Add `https://github.com/sindresorhus/Defaults` in the [“Swift Package Manager” tab in Xcode](https://developer.apple.com/documentation/xcode/adding_package_dependencies_to_your_app).
|
Add `https://github.com/sindresorhus/Defaults` in the [“Swift Package Manager” tab in Xcode](https://developer.apple.com/documentation/xcode/adding_package_dependencies_to_your_app).
|
||||||
|
|
||||||
**There are some issues running Defaults with Xcode 13.3 because of a Swift bug. [See the workaround](workaround.md).**
|
**Requires Xcode 14 or later**
|
||||||
|
|
||||||
## Support types
|
## Support types
|
||||||
|
|
||||||
|
@ -670,7 +640,7 @@ struct UserBridge: Defaults.Bridge {
|
||||||
typealias Serializable = [String: String]
|
typealias Serializable = [String: String]
|
||||||
|
|
||||||
public func serialize(_ value: Value?) -> Serializable? {
|
public func serialize(_ value: Value?) -> Serializable? {
|
||||||
guard let value = value else {
|
guard let value else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -682,7 +652,7 @@ struct UserBridge: Defaults.Bridge {
|
||||||
|
|
||||||
public func deserialize(_ object: Serializable?) -> Value? {
|
public func deserialize(_ object: Serializable?) -> Value? {
|
||||||
guard
|
guard
|
||||||
let object = object,
|
let object,
|
||||||
let name = object["name"],
|
let name = object["name"],
|
||||||
let age = object["age"]
|
let age = object["age"]
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -1,50 +0,0 @@
|
||||||
## Workaround for Xcode 13.3
|
|
||||||
|
|
||||||
When using `Defaults` with Xcode 13.3, the compiler may complain about `Type 'YourType' does not conform to protocol 'DefaultsSerializable'`.
|
|
||||||
|
|
||||||
[**This is a Swift bug.**](https://bugs.swift.org/projects/SR/issues/SR-15807)
|
|
||||||
|
|
||||||
Workaround:
|
|
||||||
|
|
||||||
1. Create a file named `Defaults+Workaround.swift` in the project using `Defaults`.
|
|
||||||
2. Copy the below code into `Defaults+Workaround.swift`.
|
|
||||||
|
|
||||||
```swift
|
|
||||||
import Foundation
|
|
||||||
import Defaults
|
|
||||||
|
|
||||||
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: Codable & NSSecureCoding & Defaults.PreferNSSecureCoding {
|
|
||||||
public static var bridge: Defaults.NSSecureCodingBridge<Self> { Defaults.NSSecureCodingBridge() }
|
|
||||||
}
|
|
||||||
|
|
||||||
extension Defaults.Serializable where Self: Codable & RawRepresentable {
|
|
||||||
public static var bridge: Defaults.RawRepresentableCodableBridge<Self> { Defaults.RawRepresentableCodableBridge() }
|
|
||||||
}
|
|
||||||
|
|
||||||
extension Defaults.Serializable where Self: Codable & RawRepresentable & Defaults.PreferRawRepresentable {
|
|
||||||
public static var bridge: Defaults.RawRepresentableBridge<Self> { Defaults.RawRepresentableBridge() }
|
|
||||||
}
|
|
||||||
|
|
||||||
extension Defaults.Serializable where Self: RawRepresentable {
|
|
||||||
public static var bridge: Defaults.RawRepresentableBridge<Self> { Defaults.RawRepresentableBridge() }
|
|
||||||
}
|
|
||||||
extension Defaults.Serializable where Self: NSSecureCoding {
|
|
||||||
public static var bridge: Defaults.NSSecureCodingBridge<Self> { Defaults.NSSecureCodingBridge() }
|
|
||||||
}
|
|
||||||
|
|
||||||
extension Defaults.CollectionSerializable where Element: Defaults.Serializable {
|
|
||||||
public static var bridge: Defaults.CollectionBridge<Self> { Defaults.CollectionBridge() }
|
|
||||||
}
|
|
||||||
|
|
||||||
extension Defaults.SetAlgebraSerializable where Element: Defaults.Serializable & Hashable {
|
|
||||||
public static var bridge: Defaults.SetAlgebraBridge<Self> { Defaults.SetAlgebraBridge() }
|
|
||||||
}
|
|
||||||
```
|
|
Loading…
Reference in New Issue