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

Implement part of the value(forKey) method #1711

merged 2 commits into from
Oct 10, 2018

Conversation

DunnCoding
Copy link
Contributor

In NSDictionary the value(forKey) method is currently unsupported.

This PR aims to implement part of this method based on the following documentation. That is the non-KVC path which calls out to the already implemented object(forKey) method.

I've added a unit test for the new method which tests that value(forKey) does return the value we expect. I've also added a test for nested dictionaries.

The KVC path, where the key has a @ prefix, has been left unsupported.

@ianpartridge
Copy link
Contributor

@swift-ci please test

let dict: NSDictionary = ["foo": "bar"]
let result = dict.value(forKey: "foo")
XCTAssert(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!

@spevans
Copy link
Contributor

spevans commented Oct 1, 2018

@swift-ci please test

@ianpartridge
Copy link
Contributor

@swift-ci please test and merge

@@ -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.

@millenomi
Copy link
Contributor

@swift-ci please test and merge

@ianpartridge
Copy link
Contributor

Yeah we found an SDK that wouldn't run on Linux because it was using this method in the non-KVC path. So I figured it was worth fixing that as it's so easy to do.

@millenomi
Copy link
Contributor

It is, but we need to be mindful of the consequences. I’m trying to figure out if there’s a way I can mark the superclass’s method as unavailable while keeping this one as available.

If the SDK is open source or the provider is amenable, it may also be useful to fix the issue upstream.

@millenomi
Copy link
Contributor

@DougGregor — this is a much smaller patch that still triggers the blocking issue, if working with mine is problematic.

@ianpartridge
Copy link
Contributor

Yes we're working with upstream to move them to object(forKey:) (which they probably meant to use anyway, understandably since dictionaries have "key"s and "value"s).

@DougGregor
Copy link
Member

@swift-ci please test

@millenomi
Copy link
Contributor

@swift-ci please test and merge

@swift-ci swift-ci merged commit e3f021c into swiftlang:master Oct 10, 2018
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.

7 participants