我如何从Swift的字典获得密钥的价值? [英] how can i get key's value from dictionary in Swift?

查看:123
本文介绍了我如何从Swift的字典获得密钥的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我早早起来快鸟。我有一个字典。我想得到我的键的value.Object的关键方法不适用于我。有人可以帮助我吗?



这是我的字典; / p>

  var companies = [AAPL:Apple Inc,GOOG:Google Inc,AMZN Amazon.com,Inc,FB:Facebook Inc] 

在company.key {

println(companies.objectForKey(AAPL )

}


解决方案

使用这种方法,您将看到密钥和值。

  var companies = [AAPL:Apple Inc,GOOG :Google Inc,AMZN:Amazon.com,Inc,FB:Facebook Inc] 

(公司中的(key,value)){
println (\(key) - > \(value))
}

或者如果您只想要的值:

 在...的价值.values.array {
println(\\ \\(价值))
}

访问字典:

  println(companies [AAPL])
/ pre>

I am early bird in swift. I have a dictionary.I want to get my key's value.Object for key method is not worked for me.Can anybody help me please?

This is my dictionary;

var companies = ["AAPL" : "Apple Inc", "GOOG" : "Google Inc", "AMZN" : "Amazon.com, Inc", "FB" : "Facebook Inc"]

    for (name) in companies.key {

println(companies.objectForKey("AAPL"))

  }

解决方案

With this method you see the key and the value.

var companies = ["AAPL" : "Apple Inc", "GOOG" : "Google Inc", "AMZN" : "Amazon.com, Inc", "FB" : "Facebook Inc"]

for (key, value) in companies {
    println("\(key) -> \(value)")
}

Or if you only want the values:

for value in companies.values.array {
    println("\(value)")
}

One value with direct access on the dictionary:

println(companies["AAPL"])

这篇关于我如何从Swift的字典获得密钥的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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