[swift-users] Dictionary with optional values

Nathan Day nathan_day at mac.com
Wed May 18 21:35:31 CDT 2016


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 situation where I had to treat the two differently with the absence of key falling back to a default value but NSNull meaning explicitly null, in swift you could have an actual nil in the dictionary.

Sent from my iPhone

> On 19 May 2016, at 6:16 AM, Jens Alfke via swift-users <swift-users at swift.org> wrote:
> 
> Thinking about it, I can’t see much use for a dictionary of optionals. What’s the difference between “x has no value” and “x has a value of nil”? I guess it’s that when you iterate the keys you see x. This seems like a tricky use that could easily confuse someone reading the code (who could be you, a year later!) Personally I’d prefer a different, clearer solution, unless this was something performance-critical that led to faster code. In which case I’d add lots of comments!


More information about the swift-users mailing list