什么是Java HashMap< String,Integer>的等价物。在斯威夫特 [英] What is the equivalent of a Java HashMap<String,Integer> in Swift

查看:198
本文介绍了什么是Java HashMap< String,Integer>的等价物。在斯威夫特的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Java编写的示例,我想将其转换为Swift。下面是代码的一部分。如果你能提供帮助我真的很感激。

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();
    ...
}

注意: entrySet()是一个 java.util的方法。映射 界面,而 getValue() java.util.Map.Entry 界面。

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

推荐答案

我相信你可以使用字典。这里有两种方法可以做字典部分。

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

或尝试使用类型推断

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

至于for循环

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

这篇关于什么是Java HashMap&lt; String,Integer&gt;的等价物。在斯威夫特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆