Skip to content

Fix UserDefaults.double(forKey:) behavior #4783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 26, 2023

Conversation

woxtu
Copy link
Contributor

@woxtu woxtu commented Jun 26, 2023

This PR fixes UserDefaults.double(forKey:) behavior for missing number values.
https://developer.apple.com/documentation/foundation/userdefaults/1416581-double

UserDefaults.standard.set(true, forKey: "key1")
UserDefaults.standard.set(42, forKey: "key2")
UserDefaults.standard.set(12.34 as Float, forKey: "key3")
UserDefaults.standard.set(12.34, forKey: "key4")
UserDefaults.standard.set("1234", forKey: "key5")

// on macOS
UserDefaults.standard.double(forKey: "key1") // 1
UserDefaults.standard.double(forKey: "key2") // 42
UserDefaults.standard.double(forKey: "key3") // 12.34000015258789
UserDefaults.standard.double(forKey: "key4") // 12.34
UserDefaults.standard.double(forKey: "key5") // 1234

// on Linux
UserDefaults.standard.double(forKey: "key1") // 0
UserDefaults.standard.double(forKey: "key2") // 0
UserDefaults.standard.double(forKey: "key3") // 0
UserDefaults.standard.double(forKey: "key4") // 12.34
UserDefaults.standard.double(forKey: "key5") // 1234

@compnerd
Copy link
Member

@woxtu would you please rebase the change?

@woxtu
Copy link
Contributor Author

woxtu commented Jul 24, 2023

Sure.

@woxtu woxtu force-pushed the userdefaults-double branch from bcb9f1e to 2f1ab88 Compare July 24, 2023 12:29
@woxtu woxtu force-pushed the userdefaults-double branch from 2f1ab88 to e9a17a3 Compare July 24, 2023 12:32
@compnerd
Copy link
Member

@swift-ci please test

@compnerd
Copy link
Member

@swift-ci please test Windows platform

@compnerd compnerd merged commit 9cf3489 into swiftlang:main Jul 26, 2023
@woxtu woxtu deleted the userdefaults-double branch March 30, 2024 11:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants