36

I've been playing around with Swift, and just came across an issue. I have the following Dictionary in:

var locations:Dictionary<String,CLLocationCoordinate2D> = ["current":CLLocationCoordinate2D(latitude: lat, longitude: lng) ];

println("current locaition is \(locations["current"])")

but the compiler is complaining about double quotes around current which represent the a key in my dictionary.

I tried escaping it with \ but it wasn't the right way.

Appreciate any help.

Amir
  • 9,577
  • 11
  • 41
  • 58
  • Isn't the type returned from `locations["current"]` a `CLLocationCoordinate2D?` as in an optional type: https://developer.apple.com/library/prerelease/ios/documentation/General/Reference/SwiftStandardLibraryReference/Dictionary.html#//apple_ref/doc/uid/TP40014608-CH4-SW7 – Chris Jun 03 '14 at 21:19

5 Answers5

42

Xcode 7.1+

Since Xcode 7.1 beta 2, we can now use quotations within string literals. From the release notes:

Expressions interpolated in strings may now contain string literals. For example, "My name is (attributes["name"]!)" is now a valid expression. (14050788)

Xcode <7.1

I don't think you can do it that way.

From the docs

The expressions you write inside parentheses within an interpolated string cannot contain an unescaped double quote (") or backslash (\), and cannot contain a carriage return or line feed.

You'd have to use

let someVar = dict["key"]
println("Some words \(someVar)")
Logan
  • 52,262
  • 20
  • 99
  • 128
  • This is such a bummer of an answer but thanks @Logan for making it gentle! – Shaun Mar 17 '15 at 14:10
  • 5
    Why would somebody downvote this? It's not my fault you can't do what you want, and I literally quoted the documentation? – Logan Jun 03 '15 at 15:24
10

You can use string concatenation instead of interpolation:

println("Some words " + dict["key"]! + " some more words.")

Just mind the spaces around the + signs.

UPDATE:

Another thing you can do is to use string format specifiers the same way how it was done back in the objective-c days:

println( String(format: "Some words %@ some more words", dict["1"]!) )
Tom Kraina
  • 3,569
  • 1
  • 38
  • 58
3

I've had this problem too with interpolating strings. The best solution I found was just to split it into two lines like so:

let location = locations["current"]
println("current locaition is \(location)")

It may be a bug with Swift. From what I found in the docs, you should be able to use \ to escape quotes.

Connor Pearson
  • 63,902
  • 28
  • 145
  • 142
  • You can use escape quotes, but not inside of parenthesis. See my answer. – Logan Jun 03 '14 at 20:59
  • 2
    That's what confuses me. It sounds like you can include escaped double quotes, but you escape them with backslash which isn't allowed. – Connor Pearson Jun 03 '14 at 21:02
  • 1
    I didn't even realize that. It's basically saying, you can't do it unless you fix it ... but also, you're not allowed to fix it. – Logan Jun 03 '14 at 21:05
3

Swift doesn't accept quote in \(). Therefore you need to separate the one-line code into two.

Here is a blog showing example for Chinese readers: http://tw.gigacircle.com/321945-1

zaph
  • 111,848
  • 21
  • 189
  • 228
0

Since Swift 2.1 you can use double quotes on interpolation. println("current location is \(locations["current"])")