Increment number in Dictionary
Asked Answered
R

4

8

I have a Dictionary [String:AnyObject] which contains some keys and values.

I want to increment a key to which value is of type Double.

I can do it this way:

let nr = dict["number"] as! Double
dict["number"] = nr + 10

But I dont like that way so Im wondering if there is another way

I tried this:

(dict["number"] as! Double) += 10

But that gives me an error:

Binary operator '+=' cannot be applied to operands of type '(Double)' and 'Double'

Why isn't this working?

Runabout answered 20/5, 2015 at 16:49 Comment(6)
Your first solution is the cleanest and most readable solution for achieving what you want. Why do you think it's wrong?Methoxychlor
@DannyBravo I dont think thts wrong Im nostly curious why the second solution doesnt work. And I like single-line stuff :)Runabout
@DannyBravo And personally I think the second solution is cleaner and more readable when put into my other code.Runabout
Fair enough. I'm not sure why the other option is not working for you, though.Methoxychlor
The alternative is dict["number"] = dict["number"]! + 10Boardwalk
@Boardwalk Fair enough :) Had to cast to Double though, thanks!Runabout
G
6

You are close and in fact you can write to a dictionary using +=, the problem is your cast. For example we can do:

var dict = ["number" : 2]
dict["number"]! += 10

and now dict["number"] returns 12. Now this is creating a new value (12) and replacing it into the dictionary, it is just a clean way of looking at it.

The problem with your code is that the left side (dict["number"] as! Double) gives you a Double. So you have say (12), then the right side is a Double too (10). So your code ends up looking like (12) += 10 which you can clearly see as problematic, since this is equivalent to 12 = 12 + 10, yikes!

So that being said, you can use the my first solution if you are working with native Swift dictionaries, otherwise your solved solution above works too, just a bit longer.

Lastly, if you are really looking for a one liner that works with your exact situation you should be able to do something like:

dict["number"] = (dict["number"] as! Double) + 10
Gnathonic answered 20/5, 2015 at 17:30 Comment(1)
Thats why I was so confused because if its a [String:Double] I know I can increment like I want, guess this is the correct answer. Sorry @matt :(Runabout
P
13

Following is an alternative. If you want to avoid force unwrapping an optional:

dict["number"] = (dict["number"] ?? 0) + 10
Poinciana answered 5/9, 2018 at 23:16 Comment(1)
I'm growing to enjoy these sorts of shortcuts in Swift, after my years in C++. Altho probably they're available in C++ now as well.Hydrophane
G
6

You are close and in fact you can write to a dictionary using +=, the problem is your cast. For example we can do:

var dict = ["number" : 2]
dict["number"]! += 10

and now dict["number"] returns 12. Now this is creating a new value (12) and replacing it into the dictionary, it is just a clean way of looking at it.

The problem with your code is that the left side (dict["number"] as! Double) gives you a Double. So you have say (12), then the right side is a Double too (10). So your code ends up looking like (12) += 10 which you can clearly see as problematic, since this is equivalent to 12 = 12 + 10, yikes!

So that being said, you can use the my first solution if you are working with native Swift dictionaries, otherwise your solved solution above works too, just a bit longer.

Lastly, if you are really looking for a one liner that works with your exact situation you should be able to do something like:

dict["number"] = (dict["number"] as! Double) + 10
Gnathonic answered 20/5, 2015 at 17:30 Comment(1)
Thats why I was so confused because if its a [String:Double] I know I can increment like I want, guess this is the correct answer. Sorry @matt :(Runabout
B
6

Another option:

dict["number", default: 0] += 10
Blate answered 15/7, 2022 at 0:4 Comment(0)
E
0

The safe way of casting would be:

if let num = dict["number"] as? Double {
    dict["number"] = num + 10
}
Esterify answered 23/2, 2019 at 20:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.