Swift:如何在字符串表达式中打印出字典键的值?

时间:2014-06-17 13:24:28

标签: dictionary swift

出于某种原因,我无法使这个表达起作用:

let expandedBio: Dictionary<String,AnyObject> = ["name":"Saurabh", "profession":"developer", "language":"java", "employed": true]

if let employed : AnyObject = expandedBio["employed"] {
    println("\(expandedBio[\"name\"]) is not available")
}

如何输出println声明?我收到了错误

Unexpected "" character  error in string interpolation

我该怎么做?

1 个答案:

答案 0 :(得分:5)

在当前版本的Swift中,你必须首先将值放在它自己的常量/变量中,然后使用它。

if let employed : AnyObject = expandedBio["employed"] {
    let t = expandedBio["name"]
    println("\(t) is not available")
}