[swift-users] Dictionary with optional values

David Sweeris davesweeris at mac.com
Wed May 18 06:27:18 CDT 2016


I'm not in front of Xcode, so I can't confirm this, but I suspect that `optDict["one"] = nil as! Int?` will set "one" to nil, rather than removing "one".

Whatever the rules for inferring the type of `nil` when an Optional<T:NilLiteralConvertible> is involved are, it seems like it always infers the one I don't want.

Hope that helps.

- Dave Sweeris

> On May 18, 2016, at 05:56, Artyom Goncharov via swift-users <swift-users at swift.org> wrote:
> 
> Hi, here is the playground snippet:
> 
> var noOptDict = ["one": 1, "two": 2, "three": 3 ]
> noOptDict["one"] = nil
> noOptDict // “one” is gone
> 
> var optDict: [String: Int?] = ["one": 1, "two": 2, "three": nil]
> optDict["one"] = nil
> optDict // “one” is gone but “three” is still there
> 
> So the first dict instance works as it should, the second using opt values allows to store nil but deletes the key when you assign nil. Is it bug, feature, or both?
> 
> Best wishes,
> Artyom
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users


More information about the swift-users mailing list