Skip to content

Fix UserDefaults.string(forKey:) behavior #4780

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 21, 2023
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
20 changes: 19 additions & 1 deletion Sources/Foundation/UserDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,25 @@ open class UserDefaults: NSObject {
}

open func string(forKey defaultName: String) -> String? {
return object(forKey: defaultName) as? String
guard let aVal = object(forKey: defaultName) else {
return nil
}
if let bVal = aVal as? String {
return bVal
}
if let bVal = aVal as? Bool {
return NSNumber(value: bVal).stringValue
}
if let bVal = aVal as? Int {
return NSNumber(value: bVal).stringValue
}
if let bVal = aVal as? Float {
return NSNumber(value: bVal).stringValue
}
if let bVal = aVal as? Double {
return NSNumber(value: bVal).stringValue
}
return nil
}

open func array(forKey defaultName: String) -> [Any]? {
Expand Down
40 changes: 40 additions & 0 deletions Tests/Foundation/Tests/TestUserDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class TestUserDefaults : XCTestCase {
("test_setValue_BoolFromString", test_setValue_BoolFromString ),
("test_setValue_IntFromString", test_setValue_IntFromString ),
("test_setValue_DoubleFromString", test_setValue_DoubleFromString ),
("test_setValue_StringFromBool", test_setValue_StringFromBool ),
("test_setValue_StringFromInt", test_setValue_StringFromInt ),
("test_setValue_StringFromFloat", test_setValue_StringFromFloat ),
("test_setValue_StringFromDouble", test_setValue_StringFromDouble ),
("test_volatileDomains", test_volatileDomains),
("test_persistentDomain", test_persistentDomain ),
]
Expand Down Expand Up @@ -245,6 +249,42 @@ class TestUserDefaults : XCTestCase {
XCTAssertEqual(defaults.double(forKey: "key1"), 12.34)
}

func test_setValue_StringFromBool() {
let defaults = UserDefaults.standard

// Register a bool default value. UserDefaults.string(forKey:) is supposed to return the converted String value
defaults.set(true, forKey: "key1")

XCTAssertEqual(defaults.string(forKey: "key1"), "1")
}

func test_setValue_StringFromInt() {
let defaults = UserDefaults.standard

// Register a int default value. UserDefaults.string(forKey:) is supposed to return the converted String value
defaults.set(42, forKey: "key1")

XCTAssertEqual(defaults.string(forKey: "key1"), "42")
}

func test_setValue_StringFromFloat() {
let defaults = UserDefaults.standard

// Register a float default value. UserDefaults.string(forKey:) is supposed to return the converted String value
defaults.set(12.34 as Float, forKey: "key1")

XCTAssertEqual(defaults.string(forKey: "key1"), "12.34")
}

func test_setValue_StringFromDouble() {
let defaults = UserDefaults.standard

// Register a double default value. UserDefaults.string(forKey:) is supposed to return the converted String value
defaults.set(12.34, forKey: "key1")

XCTAssertEqual(defaults.string(forKey: "key1"), "12.34")
}

func test_volatileDomains() {
let dateKey = "A Date",
stringKey = "A String",
Expand Down