[swift-users] Dictionary with optional values

肇鑫 owenzx at gmail.com
Wed May 18 09:35:30 CDT 2016

I think you should use updateValue forKey method instead of subscript=value.
Subscript in Dictionary returns an Element?, you should not use it like a subscript in Array, which returns  Element.

Get Outlook for iOS

From: Artyom Goncharov via swift-users <swift-users at swift.org>
Sent: 星期三, 五月 18, 2016 6:56 下午
Subject: [swift-users] Dictionary with optional values
To:  <swift-users at swift.org>

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,
swift-users mailing list
swift-users at swift.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160518/a458c9ad/attachment.html>

More information about the swift-users mailing list