Xcode 13.3 workaround (#95)

Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
This commit is contained in:
hank121314 2022-03-16 01:28:40 +08:00 committed by GitHub
parent c9198bb1d0
commit 119f654d44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 114 additions and 3 deletions

View File

@ -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. 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 {
@ -65,6 +74,9 @@ 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
} }
@ -84,6 +96,9 @@ 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 object = value else {
return nil return nil
@ -225,6 +240,9 @@ 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 init() {}
public func serialize(_ value: Value?) -> Serializable? { public func serialize(_ value: Value?) -> Serializable? {
guard let setAlgebra = value else { guard let setAlgebra = value else {
return nil return nil
@ -264,6 +282,9 @@ 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 init() {}
public func serialize(_ value: Value?) -> Serializable? { public func serialize(_ value: Value?) -> Serializable? {
guard let collection = value else { guard let collection = value else {
return nil return nil

View File

@ -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<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() }
}

View File

@ -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). 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 ## Support types
- `Int(8/16/32/64)` - `Int(8/16/32/64)`

50
workaround.md Normal file
View File

@ -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<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() }
}
```