From c9033c70cf6ba422225bbfe1265bd5a370ab0e74 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 7 Jun 2020 02:14:12 +0800 Subject: [PATCH] Minor tweaks --- Sources/Defaults/Defaults.swift | 4 ++-- Sources/Defaults/Observation+Combine.swift | 2 +- Sources/Defaults/Observation.swift | 12 ++++++------ Sources/Defaults/util.swift | 6 +++--- Tests/DefaultsTests/DefaultsTests.swift | 6 +++--- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Sources/Defaults/Defaults.swift b/Sources/Defaults/Defaults.swift index 6d3637b..b76fd8a 100644 --- a/Sources/Defaults/Defaults.swift +++ b/Sources/Defaults/Defaults.swift @@ -131,8 +131,8 @@ extension Defaults { } } -extension Defaults.Key where Value: _DefaultsOptionalType { - public convenience init(_ key: String, suite: UserDefaults = .standard) { +extension Defaults.Key { + public convenience init(_ key: String, suite: UserDefaults = .standard) where Value == T? { self.init(key, default: nil, suite: suite) } } diff --git a/Sources/Defaults/Observation+Combine.swift b/Sources/Defaults/Observation+Combine.swift index a82ed65..4a27770 100644 --- a/Sources/Defaults/Observation+Combine.swift +++ b/Sources/Defaults/Observation+Combine.swift @@ -143,7 +143,7 @@ extension Defaults { DefaultsPublisher(suite: key.suite, key: key.name, options: options) .map { _ in () } .eraseToAnyPublisher() - }.reduce(initial) { (combined, keyPublisher) in + }.reduce(initial) { combined, keyPublisher in combined.merge(with: keyPublisher).eraseToAnyPublisher() } diff --git a/Sources/Defaults/Observation.swift b/Sources/Defaults/Observation.swift index 3a28618..c83d749 100644 --- a/Sources/Defaults/Observation.swift +++ b/Sources/Defaults/Observation.swift @@ -83,11 +83,11 @@ extension Defaults { let oldValue: Any? init(change: [NSKeyValueChangeKey: Any]) { - kind = NSKeyValueChange(rawValue: change[.kindKey] as! UInt)! - indexes = change[.indexesKey] as? IndexSet - isPrior = change[.notificationIsPriorKey] as? Bool ?? false - oldValue = change[.oldKey] - newValue = change[.newKey] + self.kind = NSKeyValueChange(rawValue: change[.kindKey] as! UInt)! + self.indexes = change[.indexesKey] as? IndexSet + self.isPrior = change[.notificationIsPriorKey] as? Bool ?? false + self.oldValue = change[.oldKey] + self.newValue = change[.newKey] } } @@ -168,7 +168,7 @@ extension Defaults { lifetimeAssociation?.cancel() } - private var lifetimeAssociation: LifetimeAssociation? = nil + private var lifetimeAssociation: LifetimeAssociation? public func tieToLifetime(of weaklyHeldObject: AnyObject) -> Self { lifetimeAssociation = LifetimeAssociation(of: self, with: weaklyHeldObject, deinitHandler: { [weak self] in diff --git a/Sources/Defaults/util.swift b/Sources/Defaults/util.swift index 51ac149..0746b7a 100644 --- a/Sources/Defaults/util.swift +++ b/Sources/Defaults/util.swift @@ -79,12 +79,12 @@ final class LifetimeAssociation { - Parameter owner: The object whose lifetime extends the target object's lifetime. - Parameter deinitHandler: An optional closure to call when either `owner` or the resulting `LifetimeAssociation` is deallocated. */ - init(of target: AnyObject, with owner: AnyObject, deinitHandler: @escaping () -> Void = { }) { + init(of target: AnyObject, with owner: AnyObject, deinitHandler: @escaping () -> Void = {}) { let wrappedObject = ObjectLifetimeTracker(for: target, deinitHandler: deinitHandler) - + let associatedObjects = LifetimeAssociation.associatedObjects[owner] ?? [] LifetimeAssociation.associatedObjects[owner] = associatedObjects + [wrappedObject] - + self.wrappedObject = wrappedObject self.owner = owner } diff --git a/Tests/DefaultsTests/DefaultsTests.swift b/Tests/DefaultsTests/DefaultsTests.swift index c1d323b..bfa0fa7 100644 --- a/Tests/DefaultsTests/DefaultsTests.swift +++ b/Tests/DefaultsTests/DefaultsTests.swift @@ -366,7 +366,7 @@ final class DefaultsTests: XCTestCase { func testReceiveValueBeforeSubscriptionCombine() { let key = Defaults.Key("receiveValueBeforeSubscription", default: "hello") let expect = expectation(description: "Observation closure being called") - + let publisher = Defaults .publisher(key) .compactMap { $0.newValue } @@ -378,7 +378,7 @@ final class DefaultsTests: XCTestCase { expect.fulfill() } - Defaults[key] = "world"; + Defaults[key] = "world" cancellable.cancel() waitForExpectations(timeout: 10) } @@ -573,7 +573,7 @@ final class DefaultsTests: XCTestCase { let expect = expectation(description: "Observation closure being called") weak var observation: Defaults.Observation! - observation = Defaults.observe(key, options: []) { change in + observation = Defaults.observe(key, options: []) { _ in observation.invalidate() expect.fulfill() }.tieToLifetime(of: self)