Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ jobs:
DEVELOPER_DIR: '/Applications/Xcode_${{ matrix.xcode }}.app/Contents/Developer'
strategy:
matrix:
xcode: [ 15.4, 16.4 ]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swift-tools-version:6.0 is not supported in Xcode 15.4, so we are ending support for Xcode 15.4 in CI.

xcode: [ 16.4 ]
include:
- xcode: 15.4
macos: macos-14
- xcode: 16.4
macos: macos-15
steps:
Expand All @@ -37,11 +35,8 @@ jobs:
DEVELOPER_DIR: '/Applications/Xcode_${{ matrix.xcode }}.app/Contents/Developer'
strategy:
matrix:
xcode: [ 15.4, 16.4 ]
xcode: [ 16.4 ]
include:
- xcode: 15.4
macos: macos-14
destination: "platform=iOS Simulator,name=iPhone 15,OS=17.5"
- xcode: 16.4
macos: macos-15
destination: "platform=iOS Simulator,name=iPhone 16,OS=18.5"
Expand All @@ -56,11 +51,8 @@ jobs:
runs-on: ${{ matrix.macos }}
strategy:
matrix:
xcode: [ 15.4, 16.4 ]
xcode: [ 16.4 ]
include:
- xcode: 15.4
macos: macos-14
destination: "platform=tvOS Simulator,name=Apple TV,OS=17.5"
- xcode: 16.4
macos: macos-15
destination: "platform=tvOS Simulator,name=Apple TV,OS=18.5"
Expand All @@ -77,11 +69,8 @@ jobs:
runs-on: ${{ matrix.macos }}
strategy:
matrix:
xcode: [ 15.4, 16.4 ]
xcode: [ 16.4 ]
include:
- xcode: 15.4
macos: macos-14
destination: "platform=watchOS Simulator,name=Apple Watch Series 9 (41mm),OS=10.5"
- xcode: 16.4
macos: macos-15
destination: "platform=watchOS Simulator,name=Apple Watch Series 10 (42mm),OS=11.5"
Expand All @@ -107,6 +96,8 @@ jobs:
cocoapods:
name: CocoaPods
runs-on: macos-15
env:
DEVELOPER_DIR: '/Applications/Xcode_16.4.app/Contents/Developer'
steps:
- name: Checkout Repo
uses: actions/checkout@v4
Expand Down
8 changes: 4 additions & 4 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 14.5;
IPHONEOS_DEPLOYMENT_TARGET = 15;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
Expand Down Expand Up @@ -340,7 +340,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 14.5;
IPHONEOS_DEPLOYMENT_TARGET = 15;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = iphoneos;
Expand All @@ -359,7 +359,7 @@
DEVELOPMENT_ASSET_PATHS = "";
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = Example/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand All @@ -380,7 +380,7 @@
DEVELOPMENT_ASSET_PATHS = "";
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = Example/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
4 changes: 2 additions & 2 deletions Example/ExampleKit/Package.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// swift-tools-version:5.3
// swift-tools-version:6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "ExampleKit",
platforms: [
.iOS(.v14)
.iOS(.v15)
],
products: [
.library(name: "ExampleKit", targets: ["ExampleKit"])
Expand Down
8 changes: 4 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// swift-tools-version:5.3
// swift-tools-version:6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "swift-user-defaults",
platforms: [
.macOS(.v10_13),
.iOS(.v12),
.macOS(.v15),
.iOS(.v15),
.watchOS(.v7),
.tvOS(.v12)
.tvOS(.v15)
],
products: [
.library(name: "SwiftUserDefaults", targets: ["SwiftUserDefaults"]),
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftUserDefaults/UserDefaults+Key.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public extension UserDefaults {
///
/// let rawValue = UserDefaults.standard.string(forKey: UserDefaults.Key.userState.rawValue)
/// ```
struct Key: RawRepresentable, Hashable {
struct Key: RawRepresentable, Hashable, Sendable {
/// The underlying string value that is used for assigning a value against within the user defaults.
public let rawValue: String

Expand Down
3 changes: 1 addition & 2 deletions Sources/SwiftUserDefaults/UserDefaults+Observation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

import Foundation

private var userDefaultsObserverContext = 0

public extension UserDefaults {
/// Observes changes to the object associated with the specified key.
///
Expand Down Expand Up @@ -59,6 +57,7 @@ public extension UserDefaults {
let userDefaults: UserDefaults
let keyPath: String
let handler: (Change<Any?>) -> Void
private var userDefaultsObserverContext = 0

private(set) var isRegistered: Bool = false

Expand Down
15 changes: 5 additions & 10 deletions Tests/SwiftUserDefaultsTests/UserDefaultTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,31 +85,28 @@ final class UserDefaultTests: XCTestCase {
XCTAssertNil(userDefaults.object(forKey: "BoolKey"))
}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
func testObserver() {
let wrapper = UserDefault<String>(.init("StringKey"), store: userDefaults, defaultValue: "")

var changes: [UserDefaults.Change<String>] = []
let observer = wrapper.addObserver { changes.append($0) }
addTeardownBlock(observer.invalidate)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it seems unnecessary to use addTeardownBlock, I removed it.


wrapper.wrappedValue = "One"
wrapper.reset()
wrapper.wrappedValue = "Two"
userDefaults.x.set("Three", forKey: .init("StringKey"))

XCTAssertEqual(changes, [.initial(""), .update("One"), .update(""), .update("Two"), .update("Three")])
observer.invalidate()
}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
func testCodableWithDefault() {
let key = UserDefaults.Key("CodableKey")
let wrapper = UserDefault<Subject>(key, strategy: .json, store: userDefaults, defaultValue: Subject(value: "default"))

// Observe changes
var changes: [Subject] = []
let token = wrapper.addObserver(handler: { changes.append($0.value) })
addTeardownBlock(token.invalidate)

// Uses default
XCTAssertNil(userDefaults.object(forKey: key.rawValue))
Expand All @@ -135,17 +132,16 @@ final class UserDefaultTests: XCTestCase {
"default",
"default"
])
token.invalidate()
}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
func testCodable() {
let key = UserDefaults.Key("CodableKey")
let wrapper = UserDefault<Subject?>(key, strategy: .json, store: userDefaults)

// Observe changes
var changes: [Subject?] = []
let token = wrapper.addObserver(handler: { changes.append($0.value) })
addTeardownBlock(token.invalidate)

// nil when unset
XCTAssertNil(userDefaults.object(forKey: key.rawValue))
Expand Down Expand Up @@ -178,17 +174,16 @@ final class UserDefaultTests: XCTestCase {
"value",
nil
])
token.invalidate()
}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
func testRawRepresentableWithDefault() {
let key = UserDefaults.Key("RawRepresentableKey")
let wrapper = UserDefault<RawSubject>(key, store: userDefaults, defaultValue: .foo)

// Observe changes
var changes: [RawSubject] = []
let token = wrapper.addObserver(handler: { changes.append($0.value) })
addTeardownBlock(token.invalidate)

// Uses default
XCTAssertNil(userDefaults.object(forKey: key.rawValue))
Expand All @@ -214,17 +209,16 @@ final class UserDefaultTests: XCTestCase {
.foo,
.foo
])
token.invalidate()
}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
func testRawRepresentable() {
let key = UserDefaults.Key("RawRepresentableKey")
let wrapper = UserDefault<RawSubject?>(key, store: userDefaults)

// Observe changes
var changes: [RawSubject?] = []
let token = wrapper.addObserver(handler: { changes.append($0.value) })
addTeardownBlock(token.invalidate)

// Uses default
XCTAssertNil(userDefaults.object(forKey: key.rawValue))
Expand Down Expand Up @@ -256,5 +250,6 @@ final class UserDefaultTests: XCTestCase {
nil,
.baz
])
token.invalidate()
}
}
21 changes: 21 additions & 0 deletions Tests/SwiftUserDefaultsTests/UserDefaultsObservationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,27 @@ final class UserDefaultsObservationTests: XCTestCase {
XCTAssertEqual(changes.map(\.value) as NSArray, [nil, "Test", nil, "Default", 1] as NSArray)
XCTAssertEqual(changes.map(\.label), [.initial, .update, .update, .update, .update])
}

func testInvalidateOnDeinit() {
// Given an observer is registered
var changes: [UserDefaults.Change<Any?>] = []

var observer: UserDefaults.Observation? = userDefaults.observeObject(forKey: "TestKey") { change in
changes.append(change)
}
_ = observer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q. これは必要なコードですか

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linterエラー対策で追記しました


userDefaults.set("Test", forKey: "TestKey")

// When the observer is deallocated
observer = nil

// Then no further changes should be recorded
userDefaults.set("NewTest", forKey: "TestKey")

XCTAssertEqual(changes.map(\.value) as NSArray, [nil, "Test"] as NSArray)
XCTAssertEqual(changes.map(\.label), [.initial, .update])
}
}

private extension UserDefaults.Change {
Expand Down
6 changes: 3 additions & 3 deletions swift-user-defaults.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ Pod::Spec.new do |s|
s.source = { :git => "https://github.com/cookpad/swift-user-defaults.git", :tag => "#{s.version}" }
s.source_files = "Sources/**/*.{swift}"
s.resource_bundles = {'SwiftUserDefaults' => ['Sources/SwiftUserDefaults/PrivacyInfo.xcprivacy']}
s.swift_version = "5.3"
s.swift_version = "6.0"

s.ios.deployment_target = '12.0'
s.osx.deployment_target = '10.13'
s.ios.deployment_target = '15.0'
s.osx.deployment_target = '15.0'

# Run Unit Tests
s.test_spec 'Tests' do |test_spec|
Expand Down