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.
Since Swift 2.1 you can use double quotes on interpolation.
println("current location is \(locations["current"])")
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
You can use string concatenation instead of interpolation:
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:
I've had this problem too with interpolating strings. The best solution I found was just to split it into two lines like so:
It may be a bug with Swift. From what I found in the docs, you should be able to use \ to escape quotes.
Xcode 7.1+
Since Xcode 7.1 beta 2, we can now use quotations within string literals. From the release notes:
Xcode <7.1
I don't think you can do it that way.
From the docs
You'd have to use