Skip to content

Commit 119f654

Browse files
Xcode 13.3 workaround (#95)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent c9198bb commit 119f654

File tree

4 files changed

+114
-3
lines changed

4 files changed

+114
-3
lines changed

Sources/Defaults/Defaults+Bridge.swift

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,30 @@ extension Defaults.CodableBridge {
3737
Any `Value` that conforms to `Codable` and `Defaults.Serializable` will use `CodableBridge` to do the serialization and deserialization.
3838
*/
3939
extension Defaults {
40-
public struct TopLevelCodableBridge<Value: Codable>: CodableBridge {}
40+
public struct TopLevelCodableBridge<Value: Codable>: CodableBridge {
41+
// TODO: A temporary workaround for Xcode 13.3 compiler issue. Should remove after https://bugs.swift.org/browse/SR-15807 is fixed.
42+
public init() {}
43+
}
4144
}
4245

4346
/**
4447
`RawRepresentableCodableBridge` is needed because, for example, with `enum SomeEnum: String, Codable, Defaults.Serializable`, the compiler will be confused between `RawRepresentableBridge` and `TopLevelCodableBridge`.
4548
*/
4649
extension Defaults {
47-
public struct RawRepresentableCodableBridge<Value: RawRepresentable & Codable>: CodableBridge {}
50+
public struct RawRepresentableCodableBridge<Value: RawRepresentable & Codable>: CodableBridge {
51+
// TODO: A temporary workaround for Xcode 13.3 compiler issue. Should remove after https://bugs.swift.org/browse/SR-15807 is fixed.
52+
public init() {}
53+
}
4854
}
4955

5056
/**
5157
This exists to avoid compiler ambiguity.
5258
*/
5359
extension Defaults {
54-
public struct CodableNSSecureCodingBridge<Value: Codable & NSSecureCoding>: CodableBridge {}
60+
public struct CodableNSSecureCodingBridge<Value: Codable & NSSecureCoding>: CodableBridge {
61+
// TODO: A temporary workaround for Xcode 13.3 compiler issue. Should remove after https://bugs.swift.org/browse/SR-15807 is fixed.
62+
public init() {}
63+
}
5564
}
5665

5766
extension Defaults {
@@ -65,6 +74,9 @@ extension Defaults {
6574
public typealias Value = Value
6675
public typealias Serializable = Value.RawValue
6776

77+
// TODO: A temporary workaround for Xcode 13.3 compiler issue. Should remove after https://bugs.swift.org/browse/SR-15807 is fixed.
78+
public init() {}
79+
6880
public func serialize(_ value: Value?) -> Serializable? {
6981
value?.rawValue
7082
}
@@ -84,6 +96,9 @@ extension Defaults {
8496
public typealias Value = Value
8597
public typealias Serializable = Data
8698

99+
// TODO: A temporary workaround for Xcode 13.3 compiler issue. Should remove after https://bugs.swift.org/browse/SR-15807 is fixed.
100+
public init() {}
101+
87102
public func serialize(_ value: Value?) -> Serializable? {
88103
guard let object = value else {
89104
return nil
@@ -225,6 +240,9 @@ extension Defaults {
225240
public typealias Element = Value.Element
226241
public typealias Serializable = Any
227242

243+
// TODO: A temporary workaround for Xcode 13.3 compiler issue. Should remove after https://bugs.swift.org/browse/SR-15807 is fixed.
244+
public init() {}
245+
228246
public func serialize(_ value: Value?) -> Serializable? {
229247
guard let setAlgebra = value else {
230248
return nil
@@ -264,6 +282,9 @@ extension Defaults {
264282
public typealias Element = Value.Element
265283
public typealias Serializable = Any
266284

285+
// TODO: A temporary workaround for Xcode 13.3 compiler issue. Should remove after https://bugs.swift.org/browse/SR-15807 is fixed.
286+
public init() {}
287+
267288
public func serialize(_ value: Value?) -> Serializable? {
268289
guard let collection = value else {
269290
return nil
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// TODO: A temporary workaround for Xcode 13.3 compiler issue. Should remove after https://bugs.swift.org/browse/SR-15807 is fixed.
2+
import Foundation
3+
import Defaults
4+
5+
extension Defaults.Serializable where Self: Codable {
6+
public static var bridge: Defaults.TopLevelCodableBridge<Self> { Defaults.TopLevelCodableBridge() }
7+
}
8+
9+
extension Defaults.Serializable where Self: Codable & NSSecureCoding {
10+
public static var bridge: Defaults.CodableNSSecureCodingBridge<Self> { Defaults.CodableNSSecureCodingBridge() }
11+
}
12+
13+
extension Defaults.Serializable where Self: Codable & NSSecureCoding & Defaults.PreferNSSecureCoding {
14+
public static var bridge: Defaults.NSSecureCodingBridge<Self> { Defaults.NSSecureCodingBridge() }
15+
}
16+
17+
extension Defaults.Serializable where Self: Codable & RawRepresentable {
18+
public static var bridge: Defaults.RawRepresentableCodableBridge<Self> { Defaults.RawRepresentableCodableBridge() }
19+
}
20+
21+
extension Defaults.Serializable where Self: Codable & RawRepresentable & Defaults.PreferRawRepresentable {
22+
public static var bridge: Defaults.RawRepresentableBridge<Self> { Defaults.RawRepresentableBridge() }
23+
}
24+
25+
extension Defaults.Serializable where Self: RawRepresentable {
26+
public static var bridge: Defaults.RawRepresentableBridge<Self> { Defaults.RawRepresentableBridge() }
27+
}
28+
extension Defaults.Serializable where Self: NSSecureCoding {
29+
public static var bridge: Defaults.NSSecureCodingBridge<Self> { Defaults.NSSecureCodingBridge() }
30+
}
31+
32+
extension Defaults.CollectionSerializable where Element: Defaults.Serializable {
33+
public static var bridge: Defaults.CollectionBridge<Self> { Defaults.CollectionBridge() }
34+
}
35+
36+
extension Defaults.SetAlgebraSerializable where Element: Defaults.Serializable & Hashable {
37+
public static var bridge: Defaults.SetAlgebraBridge<Self> { Defaults.SetAlgebraBridge() }
38+
}

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ For a real-world example, see the [Plash app](https://github.com/sindresorhus/Pl
7676

7777
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).
7878

79+
**There are some issues running Defaults with Xcode 13.3 because of a Swift bug. [See the workaround](workaround.md).**
80+
7981
## Support types
8082

8183
- `Int(8/16/32/64)`

workaround.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## Workaround for Xcode 13.3
2+
3+
When using `Defaults` with Xcode 13.3, the compiler may complain about `Type 'YourType' does not conform to protocol 'DefaultsSerializable'`.
4+
5+
[**This is a Swift bug.**](https://bugs.swift.org/projects/SR/issues/SR-15807)
6+
7+
Workaround:
8+
9+
1. Create a file named `Defaults+Workaround.swift` in the project using `Defaults`.
10+
2. Copy the below code into `Defaults+Workaround.swift`.
11+
12+
```swift
13+
import Foundation
14+
import Defaults
15+
16+
extension Defaults.Serializable where Self: Codable {
17+
public static var bridge: Defaults.TopLevelCodableBridge<Self> { Defaults.TopLevelCodableBridge() }
18+
}
19+
20+
extension Defaults.Serializable where Self: Codable & NSSecureCoding {
21+
public static var bridge: Defaults.CodableNSSecureCodingBridge<Self> { Defaults.CodableNSSecureCodingBridge() }
22+
}
23+
24+
extension Defaults.Serializable where Self: Codable & NSSecureCoding & Defaults.PreferNSSecureCoding {
25+
public static var bridge: Defaults.NSSecureCodingBridge<Self> { Defaults.NSSecureCodingBridge() }
26+
}
27+
28+
extension Defaults.Serializable where Self: Codable & RawRepresentable {
29+
public static var bridge: Defaults.RawRepresentableCodableBridge<Self> { Defaults.RawRepresentableCodableBridge() }
30+
}
31+
32+
extension Defaults.Serializable where Self: Codable & RawRepresentable & Defaults.PreferRawRepresentable {
33+
public static var bridge: Defaults.RawRepresentableBridge<Self> { Defaults.RawRepresentableBridge() }
34+
}
35+
36+
extension Defaults.Serializable where Self: RawRepresentable {
37+
public static var bridge: Defaults.RawRepresentableBridge<Self> { Defaults.RawRepresentableBridge() }
38+
}
39+
extension Defaults.Serializable where Self: NSSecureCoding {
40+
public static var bridge: Defaults.NSSecureCodingBridge<Self> { Defaults.NSSecureCodingBridge() }
41+
}
42+
43+
extension Defaults.CollectionSerializable where Element: Defaults.Serializable {
44+
public static var bridge: Defaults.CollectionBridge<Self> { Defaults.CollectionBridge() }
45+
}
46+
47+
extension Defaults.SetAlgebraSerializable where Element: Defaults.Serializable & Hashable {
48+
public static var bridge: Defaults.SetAlgebraBridge<Self> { Defaults.SetAlgebraBridge() }
49+
}
50+
```

0 commit comments

Comments
 (0)