Skip to content

Commit

Permalink
Xcode 13.3 workaround (#95)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
hank121314 and sindresorhus authored Mar 15, 2022
1 parent c9198bb commit 119f654
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 3 deletions.
27 changes: 24 additions & 3 deletions Sources/Defaults/Defaults+Bridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<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`.
*/
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.
*/
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 {
Expand All @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions Tests/DefaultsTests/DefaultsTests+Workaround.swift
Original file line number Diff line number Diff line change
@@ -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() }
}
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand Down
50 changes: 50 additions & 0 deletions workaround.md
Original file line number Diff line number Diff line change
@@ -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() }
}
```

0 comments on commit 119f654

Please sign in to comment.