Skip to content

Commit

Permalink
Fix UserDefaults.string(forKey:) behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
woxtu authored and compnerd committed Jul 21, 2023
1 parent bd1c1ee commit c0cd1d1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
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

0 comments on commit c0cd1d1

Please sign in to comment.