What is the equivalent of a Java HashMap<String,Integer> in Swift

Juventus :

I have an example written in Java that I would like to convert into Swift. Below is a section of the code. I would really appreciate if you can help.

Map<String, Integer> someProtocol = new HashMap<>();
someProtocol.put("one", Integer.valueOf(1));
someProtocol.put("two", Integer.valueOf(2));

for (Map.Entry<String, Integer> e : someProtocol.entrySet() {
    int index = e.getValue();
    ...
}

NOTE: entrySet() is a method of the java.util.Map interface whereas getValue() is a method of the java.util.Map.Entry interface.

Dew Time :

I believe you can use a dictionary. Here are two ways to do the dictionary part.

var someProtocol = [String : Int]()
someProtocol["one"] = 1
someProtocol["two"] = 2

or try this which uses type inference

var someProtocol = [
    "one" : 1,
    "two" : 2
]

as for the for loop

var index: Int
for (e, value) in someProtocol  {
    index = value
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=430896&siteId=1