Skip to content

Commit c2f1734

Browse files
authored
Merge pull request #4338 from jckarter/nsmutabledictionary-overlay-subscript
Foundation overlay: Fix optionality mismatch in NSMutableDictionary subscript.
2 parents 6bce1d4 + e834693 commit c2f1734

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

stdlib/public/SDK/Foundation/Foundation.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,12 @@ extension NSMutableDictionary {
10071007
set {
10081008
// FIXME: Unfortunate that the `NSCopying` check has to be done at
10091009
// runtime.
1010-
self.setObject(newValue, forKey: key as AnyObject as! NSCopying)
1010+
let copyingKey = key as AnyObject as! NSCopying
1011+
if let newValue = newValue {
1012+
self.setObject(newValue, forKey: copyingKey)
1013+
} else {
1014+
self.removeObject(forKey: copyingKey)
1015+
}
10111016
}
10121017
}
10131018
}

test/1_stdlib/NSDictionary.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,13 @@ tests.test("copy construction") {
1717
let y = NSMutableDictionary(dictionary: expected as NSDictionary)
1818
expectEqual(expected, y as NSDictionary as! Dictionary)
1919
}
20+
// rdar://problem/27875914
21+
tests.test("subscript with Any") {
22+
let d = NSMutableDictionary()
23+
d["k"] = "@this is how the world ends"
24+
expectEqual((d["k"]! as AnyObject).characterAtIndex(0), 0x40)
25+
d["k"] = nil
26+
expectTrue(d["k"] == nil)
27+
}
2028

2129
runAllTests()

0 commit comments

Comments
 (0)