键/值对在Swift中的字典中的名称 [英] key/value pairs by name in Dictionary in Swift

查看:138
本文介绍了键/值对在Swift中的字典中的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以访问 code> 键输入& ,而不是 0 1



在示例中:

  let rockyPlanets:Dictionary< String,Int> = [
//按字母顺序
地球:3,火星:4,水星:1,金星:2
]
为rockyPlanets {
println(\(eachPlanet.0)is at position \(eachPlanet.1))
}

如何以(不是索引)以这种方式访问​​键/值:

$在rockyPlanets {
println(\(eachPlanet.key)位于位置\(eachPlanet.value))的eachPlanet中,b
$ b

  
}


解决方案

  for someDict {
// ...
} b $ b

在您的情况下:

  for(planet,position)in rockyPlanets {
println(\(planet)is at position \(position))
}


Is it possible to access the key and the value of single Dictionary entry by key & value, not 0 & 1?

In the example:

let rockyPlanets: Dictionary<String, Int> = [
    // Alphabetical order
    "Earth": 3, "Mars": 4, "Mercury": 1, "Venus": 2
]
for eachPlanet in rockyPlanets {
    println("\(eachPlanet.0) is at position \(eachPlanet.1)")
}

How can I access the key/values by key or value (not index) in this fashion:

for eachPlanet in rockyPlanets {
    println("\(eachPlanet.key) is at position \(eachPlanet.value)")
}

解决方案

Is this what you are looking for?

for (key, value) in someDict {
    // ...
}

In your case:

for (planet, position) in rockyPlanets {
    println("\(planet) is at position \(position)")
}

这篇关于键/值对在Swift中的字典中的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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