Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
570 views
in Technique[技术] by (71.8m points)

escape dictionary key double quotes when doing println dictionary item in Swift

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...