4

For some reason, I just can't make this expression work:

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")
}

How do I output the println statement? I get the error

Unexpected "" character  error in string interpolation

How do I do this right?

Amit Erandole
  • 11,995
  • 23
  • 65
  • 103

1 Answers1

5

In current version of Swift you have to put value in its own constant/variable first, then use that.

if let employed : AnyObject = expandedBio["employed"] {
    let t = expandedBio["name"]
    println("\(t) is not available")
}
Kreiri
  • 7,840
  • 5
  • 30
  • 36
  • 1
    I had to use `let t : AnyObject? = expandedBio["name"]` – Amit Erandole Jun 17 '14 at 13:29
  • not necessarily but recommended for a better readability. – seafoxx Jul 23 '14 at 20:44
  • While the "if let employed" already expects that "employed" might be nil (and so can handle that situation), the simple assignment of "t" might result in a nil value -> "Because it is possible to request a key for which no value exists, a dictionary's subscript returns an optional value of the dictionary's value type." -> Thus you should make that optional clear by defining t as "AnyObject?" – seafoxx Jul 23 '14 at 20:52
  • This problem is solved in swift 2.1 – pabloa98 Dec 18 '15 at 17:05