diff --git a/Sources/Defaults/Defaults+Bridge.swift b/Sources/Defaults/Defaults+Bridge.swift index 760bf79..e65c38d 100644 --- a/Sources/Defaults/Defaults+Bridge.swift +++ b/Sources/Defaults/Defaults+Bridge.swift @@ -37,21 +37,30 @@ extension Defaults.CodableBridge { Any `Value` that conforms to `Codable` and `Defaults.Serializable` will use `CodableBridge` to do the serialization and deserialization. */ extension Defaults { - public struct TopLevelCodableBridge: CodableBridge {} + public struct TopLevelCodableBridge: 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`. */ extension Defaults { - public struct RawRepresentableCodableBridge: CodableBridge {} + public struct RawRepresentableCodableBridge: 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. */ extension Defaults { - public struct CodableNSSecureCodingBridge: CodableBridge {} + public struct CodableNSSecureCodingBridge: 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 { @@ -65,6 +74,9 @@ extension Defaults { public typealias Value = Value 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? { value?.rawValue } @@ -84,6 +96,9 @@ extension Defaults { public typealias Value = Value 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? { guard let object = value else { return nil @@ -225,6 +240,9 @@ extension Defaults { public typealias Element = Value.Element 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 init() {} + public func serialize(_ value: Value?) -> Serializable? { guard let setAlgebra = value else { return nil @@ -264,6 +282,9 @@ extension Defaults { public typealias Element = Value.Element 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 init() {} + public func serialize(_ value: Value?) -> Serializable? { guard let collection = value else { return nil diff --git a/Tests/DefaultsTests/DefaultsTests+Workaround.swift b/Tests/DefaultsTests/DefaultsTests+Workaround.swift new file mode 100644 index 0000000..d920a40 --- /dev/null +++ b/Tests/DefaultsTests/DefaultsTests+Workaround.swift @@ -0,0 +1,38 @@ +// 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 { Defaults.TopLevelCodableBridge() } +} + +extension Defaults.Serializable where Self: Codable & NSSecureCoding { + public static var bridge: Defaults.CodableNSSecureCodingBridge { Defaults.CodableNSSecureCodingBridge() } +} + +extension Defaults.Serializable where Self: Codable & NSSecureCoding & Defaults.PreferNSSecureCoding { + public static var bridge: Defaults.NSSecureCodingBridge { Defaults.NSSecureCodingBridge() } +} + +extension Defaults.Serializable where Self: Codable & RawRepresentable { + public static var bridge: Defaults.RawRepresentableCodableBridge { Defaults.RawRepresentableCodableBridge() } +} + +extension Defaults.Serializable where Self: Codable & RawRepresentable & Defaults.PreferRawRepresentable { + public static var bridge: Defaults.RawRepresentableBridge { Defaults.RawRepresentableBridge() } +} + +extension Defaults.Serializable where Self: RawRepresentable { + public static var bridge: Defaults.RawRepresentableBridge { Defaults.RawRepresentableBridge() } +} +extension Defaults.Serializable where Self: NSSecureCoding { + public static var bridge: Defaults.NSSecureCodingBridge { Defaults.NSSecureCodingBridge() } +} + +extension Defaults.CollectionSerializable where Element: Defaults.Serializable { + public static var bridge: Defaults.CollectionBridge { Defaults.CollectionBridge() } +} + +extension Defaults.SetAlgebraSerializable where Element: Defaults.Serializable & Hashable { + public static var bridge: Defaults.SetAlgebraBridge { Defaults.SetAlgebraBridge() } +} diff --git a/readme.md b/readme.md index eb95e82..4c05f7a 100644 --- a/readme.md +++ b/readme.md @@ -76,6 +76,8 @@ 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). +**There are some issues running Defaults with Xcode 13.3 because of a Swift bug. [See the workaround](workaround.md).** + ## Support types - `Int(8/16/32/64)` diff --git a/workaround.md b/workaround.md new file mode 100644 index 0000000..c4cd80c --- /dev/null +++ b/workaround.md @@ -0,0 +1,50 @@ +## 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 { Defaults.TopLevelCodableBridge() } +} + +extension Defaults.Serializable where Self: Codable & NSSecureCoding { + public static var bridge: Defaults.CodableNSSecureCodingBridge { Defaults.CodableNSSecureCodingBridge() } +} + +extension Defaults.Serializable where Self: Codable & NSSecureCoding & Defaults.PreferNSSecureCoding { + public static var bridge: Defaults.NSSecureCodingBridge { Defaults.NSSecureCodingBridge() } +} + +extension Defaults.Serializable where Self: Codable & RawRepresentable { + public static var bridge: Defaults.RawRepresentableCodableBridge { Defaults.RawRepresentableCodableBridge() } +} + +extension Defaults.Serializable where Self: Codable & RawRepresentable & Defaults.PreferRawRepresentable { + public static var bridge: Defaults.RawRepresentableBridge { Defaults.RawRepresentableBridge() } +} + +extension Defaults.Serializable where Self: RawRepresentable { + public static var bridge: Defaults.RawRepresentableBridge { Defaults.RawRepresentableBridge() } +} +extension Defaults.Serializable where Self: NSSecureCoding { + public static var bridge: Defaults.NSSecureCodingBridge { Defaults.NSSecureCodingBridge() } +} + +extension Defaults.CollectionSerializable where Element: Defaults.Serializable { + public static var bridge: Defaults.CollectionBridge { Defaults.CollectionBridge() } +} + +extension Defaults.SetAlgebraSerializable where Element: Defaults.Serializable & Hashable { + public static var bridge: Defaults.SetAlgebraBridge { Defaults.SetAlgebraBridge() } +} +```