Skip to content

Implement part of the value(forKey) method #1711

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 2 commits into from
Oct 10, 2018
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
7 changes: 6 additions & 1 deletion Foundation/NSDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
}

open func value(forKey key: String) -> Any? {
NSUnsupported()
if key.hasPrefix("@") {
Copy link
Contributor

@millenomi millenomi Oct 3, 2018

Choose a reason for hiding this comment

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

It is part of our API, but I'm kinda conflicted bringing this in; as it is, we do not have the ability to implement the full contract of KVC in Swift, and I don't know if the presence of a method is dangerous enough to lead people to use it for its full contract.

I would probably at the very least have some sort of diagnostic (via deprecation maybe?) on usage of this method. But that's for another patch.

NSUnsupported()
} else {
return object(forKey: key)
}

}

open func keyEnumerator() -> NSEnumerator {
Expand Down
15 changes: 15 additions & 0 deletions TestFoundation/TestNSDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class TestNSDictionary : XCTestCase {
("test_writeToFile", test_writeToFile),
("test_initWithContentsOfFile", test_initWithContentsOfFile),
("test_settingWithStringKey", test_settingWithStringKey),
("test_valueForKey", test_valueForKey),
("test_valueForKeyWithNestedDict", test_valueForKeyWithNestedDict),
]
}

Expand Down Expand Up @@ -217,6 +219,19 @@ class TestNSDictionary : XCTestCase {
// has crashed in the past
dict["stringKey"] = "value"
}

func test_valueForKey() {
let dict: NSDictionary = ["foo": "bar"]
let result = dict.value(forKey: "foo")
XCTAssertEqual(result as? String, "bar")
}
Copy link
Contributor

@spevans spevans Oct 1, 2018

Choose a reason for hiding this comment

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

Not a big issue buts it's generally better to use XCTAssertEqual() as it shows the incorrect value if the test fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will update, thanks!


func test_valueForKeyWithNestedDict() {
let dict: NSDictionary = ["foo": ["bar": "baz"]]
let result = dict.value(forKey: "foo")
let expectedResult: NSDictionary = ["bar": "baz"]
XCTAssertEqual(result as? NSDictionary, expectedResult)
}

private func createTestFile(_ path: String, _contents: Data) -> String? {
let tempDir = NSTemporaryDirectory() + "TestFoundation_Playground_" + NSUUID().uuidString + "/"
Expand Down