Upgrade to Swit 5.7

This commit is contained in:
Sindre Sorhus 2022-09-05 14:05:41 +07:00
parent 981ccb0a01
commit bc1af5d872
21 changed files with 70 additions and 212 deletions

View File

@ -4,12 +4,12 @@ on:
- pull_request
jobs:
test:
runs-on: macos-11
runs-on: macos-12
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- run: swift test
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: norio-nomura/action-swiftlint@3.2.1

View File

@ -1,4 +1,4 @@
// swift-tools-version:5.5
// swift-tools-version:5.7
import PackageDescription
let package = Package(

View File

@ -8,7 +8,7 @@ import UIKit
extension Defaults.CodableBridge {
public func serialize(_ value: Value?) -> Serializable? {
guard let value = value else {
guard let value else {
return nil
}
@ -25,11 +25,11 @@ extension Defaults.CodableBridge {
}
public func deserialize(_ object: Serializable?) -> Value? {
guard let jsonString = object else {
guard let object else {
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.
*/
extension Defaults {
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() {}
}
public struct TopLevelCodableBridge<Value: Codable>: CodableBridge {}
}
/**
`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 {
// 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 struct RawRepresentableCodableBridge<Value: RawRepresentable & Codable>: CodableBridge {}
}
/**
This exists to avoid compiler ambiguity.
*/
extension Defaults {
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() {}
}
public struct CodableNSSecureCodingBridge<Value: Codable & NSSecureCoding>: CodableBridge {}
}
extension Defaults {
@ -74,19 +65,16 @@ 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
}
public func deserialize(_ object: Serializable?) -> Value? {
guard let rawValue = object else {
guard let object else {
return nil
}
return Value(rawValue: rawValue)
return Value(rawValue: object)
}
}
}
@ -96,24 +84,21 @@ 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 {
guard let value else {
return nil
}
return try? NSKeyedArchiver.archivedData(withRootObject: object, requiringSecureCoding: true)
return try? NSKeyedArchiver.archivedData(withRootObject: value, requiringSecureCoding: true)
}
public func deserialize(_ object: Serializable?) -> Value? {
guard let data = object else {
guard let object else {
return nil
}
do {
return try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? Value
return try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(object) as? Value
} catch {
print(error)
return nil
@ -150,8 +135,8 @@ extension Defaults {
return array.map { Element.bridge.serialize($0) }.compact()
}
public func deserialize(_ object: Serializable?) -> Value? {
guard let array = object else {
public func deserialize(_ array: Serializable?) -> Value? {
guard let array else {
return nil
}
@ -165,8 +150,8 @@ extension Defaults {
public typealias Value = [Key: Element.Value]
public typealias Serializable = [String: Element.Serializable]
public func serialize(_ value: Value?) -> Serializable? {
guard let dictionary = value else {
public func serialize(_ dictionary: Value?) -> Serializable? {
guard let dictionary else {
return nil
}
@ -176,8 +161,8 @@ extension Defaults {
}
}
public func deserialize(_ object: Serializable?) -> Value? {
guard let dictionary = object else {
public func deserialize(_ dictionary: Serializable?) -> Value? {
guard let dictionary else {
return nil
}
@ -201,8 +186,8 @@ extension Defaults {
public typealias Value = Set<Element>
public typealias Serializable = Any
public func serialize(_ value: Value?) -> Serializable? {
guard let set = value else {
public func serialize(_ set: Value?) -> Serializable? {
guard let set else {
return nil
}
@ -240,11 +225,8 @@ 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 {
public func serialize(_ setAlgebra: Value?) -> Serializable? {
guard let setAlgebra else {
return nil
}
@ -282,11 +264,8 @@ 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 {
public func serialize(_ collection: Value?) -> Serializable? {
guard let collection else {
return nil
}
@ -328,7 +307,7 @@ extension Defaults {
}
public func deserialize(_ object: Serializable?) -> Value? {
guard let object = object else {
guard let object else {
return nil
}
@ -344,7 +323,7 @@ extension Defaults {
typealias Bound = T.Bound
public func serialize(_ value: Value?) -> Serializable? {
guard let value = value else {
guard let value else {
return nil
}
@ -363,7 +342,7 @@ extension Defaults {
}
public func deserialize(_ object: Serializable?) -> Value? {
guard let object = object else {
guard let object else {
return nil
}
@ -408,7 +387,7 @@ extension Defaults {
#endif
public func serialize(_ value: Value?) -> Serializable? {
guard let value = value else {
guard let value else {
return nil
}

View File

@ -57,7 +57,7 @@ struct UserBridge: Defaults.Bridge {
typealias Serializable = [String: String]
func serialize(_ value: Value?) -> Serializable? {
guard let value = value else {
guard let value else {
return nil
}
@ -69,7 +69,7 @@ struct UserBridge: Defaults.Bridge {
func deserialize(_ object: Serializable?) -> Value? {
guard
let object = object,
let object,
let username = object["username"],
let password = object["password"]
else {

View File

@ -198,7 +198,7 @@ extension Defaults.SetAlgebraSerializable where Self: Defaults.NativeType, Eleme
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 {
NativeForm(rawValue: rawValue)!
}

View File

@ -11,8 +11,7 @@ So we can convert the JSON string into a `NativeType` like this:
```
guard
let jsonString = string,
let jsonData = jsonString.data(using: .utf8),
let jsonData = string?.data(using: .utf8),
let codable = try? JSONDecoder().decode(NativeType.CodableForm.self, from: jsonData)
else {
return nil

View File

@ -3,8 +3,7 @@ import Foundation
extension UserDefaults {
func migrateCodableToNative<Value: Defaults.Serializable & Codable>(forKey key: String, of type: Value.Type) {
guard
let jsonString = string(forKey: key),
let jsonData = jsonString.data(using: .utf8),
let jsonData = string(forKey: key)?.data(using: .utf8),
let codable = try? JSONDecoder().decode(Value.self, from: jsonData)
else {
return
@ -38,8 +37,7 @@ extension UserDefaults {
*/
func migrateCodableToNative<Value: Defaults.NativeType>(forKey key: String, of type: Value.Type) {
guard
let jsonString = string(forKey: key),
let jsonData = jsonString.data(using: .utf8),
let jsonData = string(forKey: key)?.data(using: .utf8),
let codable = try? JSONDecoder().decode(Value.CodableForm.self, from: jsonData)
else {
return

View File

@ -58,7 +58,7 @@ extension Defaults {
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(
subscriber: subscriber,
suite: suite,

View File

@ -43,7 +43,7 @@ extension Defaults {
private static func deserialize<Value: Serializable>(_ value: Any?, to type: Value.Type) -> Value? {
guard
let value = value,
let value,
!(value is NSNull)
else {
return nil

View File

@ -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)?
private let label: () -> Label
@ -176,7 +176,7 @@ extension Defaults {
// Intentionally using `@ObservedObjected` over `@StateObject` so that the key can be dynamically changed.
@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.observable = Defaults.Observable(key)
}
@ -191,8 +191,8 @@ extension Defaults {
}
@available(macOS 11, iOS 14, tvOS 14, watchOS 7, *)
extension Defaults.Toggle where Label == Text {
public init<S>(_ title: S, key: Defaults.Key<Bool>) where S: StringProtocol {
extension Defaults.Toggle<Text> {
public init(_ title: some StringProtocol, key: Defaults.Key<Bool>) {
self.label = { Text(title) }
self.observable = Defaults.Observable(key)
}

View File

@ -104,8 +104,8 @@ final class LifetimeAssociation {
private func invalidate() {
guard
let owner = owner,
let wrappedObject = wrappedObject,
let owner,
let wrappedObject,
var associatedObjects = Self.associatedObjects[owner],
let wrappedObjectAssociationIndex = associatedObjects.firstIndex(where: { $0 === wrappedObject })
else {

View File

@ -15,7 +15,7 @@ private struct ItemBridge: Defaults.Bridge {
typealias Value = Item
typealias Serializable = [String: String]
func serialize(_ value: Value?) -> Serializable? {
guard let value = value else {
guard let value else {
return nil
}
@ -24,7 +24,7 @@ private struct ItemBridge: Defaults.Bridge {
func deserialize(_ object: Serializable?) -> Value? {
guard
let object = object,
let object,
let name = object["name"],
let count = UInt(object["count"] ?? "0")
else {

View File

@ -16,7 +16,7 @@ public final class DefaultsUserBridge: Defaults.Bridge {
public typealias Serializable = [String: String]
public func serialize(_ value: Value?) -> Serializable? {
guard let value = value else {
guard let value else {
return nil
}
@ -25,7 +25,7 @@ public final class DefaultsUserBridge: Defaults.Bridge {
public func deserialize(_ object: Serializable?) -> Value? {
guard
let object = object,
let object,
let username = object["username"],
let password = object["password"]
else {

View File

@ -52,7 +52,7 @@ private struct TimeZoneBridge: Defaults.Bridge {
typealias Serializable = [String: Any]
func serialize(_ value: TimeZone?) -> Serializable? {
guard let value = value else {
guard let value else {
return nil
}
@ -61,9 +61,9 @@ private struct TimeZoneBridge: Defaults.Bridge {
func deserialize(_ object: Serializable?) -> TimeZone? {
guard
let dictionary = object,
let id = dictionary["id"] as? String,
let name = dictionary["name"] as? String
let object,
let id = object["id"] as? String,
let name = object["name"] as? String
else {
return nil
}
@ -86,7 +86,7 @@ private struct ChosenTimeZoneBridge: Defaults.Bridge {
typealias Serializable = [String: Any]
func serialize(_ value: Value?) -> Serializable? {
guard let value = value else {
guard let value else {
return nil
}
@ -95,9 +95,9 @@ private struct ChosenTimeZoneBridge: Defaults.Bridge {
func deserialize(_ object: Serializable?) -> Value? {
guard
let dictionary = object,
let id = dictionary["id"] as? String,
let name = dictionary["name"] as? String
let object,
let id = object["id"] as? String,
let name = object["name"] as? String
else {
return nil
}

View File

@ -14,7 +14,7 @@ extension CustomDate: Defaults.Serializable {
public typealias Serializable = [Int]
public func serialize(_ value: Value?) -> Serializable? {
guard let value = value else {
guard let value else {
return nil
}
@ -22,7 +22,7 @@ extension CustomDate: Defaults.Serializable {
}
public func deserialize(_ object: Serializable?) -> Value? {
guard let object = object else {
guard let object else {
return nil
}

View File

@ -15,7 +15,7 @@ private struct ItemBridge: Defaults.Bridge {
typealias Value = Item
typealias Serializable = [String: String]
func serialize(_ value: Value?) -> Serializable? {
guard let value = value else {
guard let value else {
return nil
}
@ -24,7 +24,7 @@ private struct ItemBridge: Defaults.Bridge {
func deserialize(_ object: Serializable?) -> Value? {
guard
let object = object,
let object,
let name = object["name"],
let count = UInt(object["count"] ?? "0")
else {

View File

@ -7,7 +7,7 @@ struct DefaultsSetAlgebra<Element: Defaults.Serializable & Hashable>: SetAlgebra
init() {}
init<S: Sequence>(_ sequence: __owned S) where Element == S.Element {
init(_ sequence: __owned some Sequence<Element>) {
self.store = Set(sequence)
}

View File

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

View File

@ -331,7 +331,7 @@ private struct TimeZoneBridge: Defaults.Bridge {
typealias Serializable = [String: String]
func serialize(_ value: TimeZone?) -> Serializable? {
guard let value = value else {
guard let value else {
return nil
}
@ -343,9 +343,9 @@ private struct TimeZoneBridge: Defaults.Bridge {
func deserialize(_ object: Serializable?) -> TimeZone? {
guard
let dictionary = object,
let id = dictionary["id"],
let name = dictionary["name"]
let object,
let id = object["id"],
let name = object["name"]
else {
return nil
}

View File

@ -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.
- Comes with a convenience SwiftUI `Toggle` component.
## Compatibility
- macOS 10.13+
@ -39,35 +38,6 @@ For a real-world example, see the [Plash app](https://github.com/sindresorhus/Pl
- tvOS 12+
- 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
#### [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).
**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
@ -670,7 +640,7 @@ struct UserBridge: Defaults.Bridge {
typealias Serializable = [String: String]
public func serialize(_ value: Value?) -> Serializable? {
guard let value = value else {
guard let value else {
return nil
}
@ -682,7 +652,7 @@ struct UserBridge: Defaults.Bridge {
public func deserialize(_ object: Serializable?) -> Value? {
guard
let object = object,
let object,
let name = object["name"],
let age = object["age"]
else {

View File

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