在 Swift 中执行 println 字典项时转义字典键双引号 [英] escape dictionary key double quotes when doing println dictionary item in Swift

查看:22
本文介绍了在 Swift 中执行 println 字典项时转义字典键双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩 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"])")

但是编译器抱怨 current 周围的双引号代表我字典中的 a 键.

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.

感谢任何帮助.

推荐答案

Xcode 7.1+

从 Xcode 7.1 beta 2 开始,我们现在可以在字符串文字中使用引号.来自发行说明:

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

插入到字符串中的表达式现在可能包含字符串文字.例如,My name is (attributes["name"]!)"现在是一个有效的表达式.(14050788)

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.

来自 文档

您在内插字符串的括号内编写的表达式不能包含未转义的双引号 (") 或反斜杠 (\),并且不能包含回车或换行符.

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.

你必须使用

let someVar = dict["key"]
println("Some words \(someVar)")

这篇关于在 Swift 中执行 println 字典项时转义字典键双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆