我一直在玩Swift,刚遇到一个问题.我有以下字典:
var locations:Dictionary= ["current":CLLocationCoordinate2D(latitude: lat, longitude: lng) ]; println("current locaition is \(locations["current"])")
但编译器抱怨双引号current
代表我字典中的一个键.
我试图逃避它,\
但这不是正确的方法.
感谢任何帮助.
Xcode 7.1+
从Xcode 7.1 beta 2开始,我们现在可以在字符串文字中使用引号.从发行说明:
以字符串形式插入的表达式现在可以包含字符串文字.例如,"我的名字是(attributes ["name"]!)"现在是一个有效的表达式.(14050788)
Xcode <7.1
我不认为你能这样做.
来自文档
您在插值字符串中的括号内写的表达式不能包含未转义的双引号(")或反斜杠(\),并且不能包含回车符或换行符.
你必须使用
let someVar = dict["key"] println("Some words \(someVar)")
您可以使用字符串连接而不是插值:
println("Some words " + dict["key"]! + " some more words.")
请注意+
标志周围的空间.
更新:
您可以做的另一件事是使用字符串格式说明符,就像在Objective-c天中完成它一样:
println( String(format: "Some words %@ some more words", dict["1"]!) )