Skip to content

Fix UserDefaults.float(forKey:) behavior #4782

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 22, 2023

Conversation

woxtu
Copy link
Contributor

@woxtu woxtu commented Jun 25, 2023

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

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.float(forKey: "key1") // 1
UserDefaults.standard.float(forKey: "key2") // 42
UserDefaults.standard.float(forKey: "key3") // 12.34
UserDefaults.standard.float(forKey: "key4") // 12.34
UserDefaults.standard.float(forKey: "key5") // 1234

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

@compnerd
Copy link
Member

@swift-ci please test

@compnerd compnerd merged commit 16f93e7 into swiftlang:main Jul 22, 2023
@woxtu woxtu deleted the userdefaults-float branch July 24, 2023 12:11
@woxtu
Copy link
Contributor Author

woxtu commented Jul 24, 2023

Thanks!

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