Re: [swift-users] Dictionary with optional values

2016-05-19 Thread Jeremy Pereira via swift-users
> On 18 May 2016, at 11:56, Artyom Goncharov via swift-users > 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":

Re: [swift-users] Dictionary with optional values

2016-05-19 Thread Jens Alfke via swift-users
> On May 18, 2016, at 7:35 PM, Nathan Day wrote: > > In objective-c I have come across something like this a lot where a > NSDictionary has been created from JSON an a NSNull is used to represent an > actual null in the source JSON versus the absence of the key Yeah, this

Re: [swift-users] Dictionary with optional values

2016-05-18 Thread Nathan Day via swift-users
In objective-c I have come across something like this a lot where a NSDictionary has been created from JSON an a NSNull is used to represent an actual null in the source JSON versus the absence of the key, most of the time I have had to just convert the NSNull to a nil, but I did have a

Re: [swift-users] Dictionary with optional values

2016-05-18 Thread Jordan Rose via swift-users
> On May 18, 2016, at 09:38, Ray Fix via swift-users > wrote: > > >> On May 18, 2016, at 3:56 AM, Artyom Goncharov via swift-users >> wrote: >> >> var noOptDict = ["one": 1, "two": 2, "three": 3 ] >> noOptDict["one"] = nil > > Wow,

Re: [swift-users] Dictionary with optional values

2016-05-18 Thread Marco S Hyman via swift-users
> On May 18, 2016, at 11:03 AM, Artyom Goncharov via swift-users > wrote: > > Yes, of course I can use API method but this kind of behaviour for subscript > operator seems inconsistent(or even magical) to me because it is possible to > initialise a dictionary with nil

Re: [swift-users] Dictionary with optional values

2016-05-18 Thread Jens Alfke via swift-users
> On May 18, 2016, at 11:03 AM, Artyom Goncharov via swift-users > wrote: > > Yes, of course I can use API method but this kind of behaviour for subscript > operator seems inconsistent(or even magical) to me because it is possible to > initialise a dictionary with nil

[swift-users] Dictionary with optional values

2016-05-18 Thread Artyom Goncharov via swift-users
g a key that is not present in `self` yields `nil`. > /// Writing `nil` as the value for a given key erases that key from > /// `self`. > > Which is exactly what it is doing. As the Zhaoxin said, you can use > updateValue (and removeValueForKey) to get better results when

Re: [swift-users] Dictionary with optional values

2016-05-18 Thread David Sweeris via swift-users
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 is involved are, it seems like it always infers the one I don't want.

[swift-users] Dictionary with optional values

2016-05-18 Thread Artyom Goncharov via swift-users
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