在Swift中添加引号 [英] Adding Quotes in Swift

查看:403
本文介绍了在Swift中添加引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swift中有一种直接的方法可以将引号添加到字符串中吗?引号应正确本地化(请参阅 https://en.wikipedia.org/wiki/Quotation_mark )基于用户的语言设置。我想在添加引号后在UILabel中显示字符串。

Is there a straightforward way in Swift for adding quotation marks to a String? The quotation marks should localize properly (see https://en.wikipedia.org/wiki/Quotation_mark) based on the user's language settings. I'd like to show the string in a UILabel after adding the quotes.

例如:

var quote: String!
quote = "To be or not to be..."
// one or more lines of code that add localized quotation marks 

对于法国用户:«成为或不成为......»

对于德国用户:成为或不成为......

推荐答案

使用来自 http://nshipster.com/nslocale/ 的信息:

let locale = NSLocale.currentLocale()
let qBegin = locale.objectForKey(NSLocaleQuotationBeginDelimiterKey) as? String ?? "\""
let qEnd = locale.objectForKey(NSLocaleQuotationEndDelimiterKey) as? String ?? "\""

let quote = qBegin + "To be or not to be..." + qEnd
print(quote)

示例结果:


Locale   Output

 de      „To be or not to be..."
 en      "To be or not to be..."
 fr      «To be or not to be...»
 ja      「To be or not to be...」

我不知道
语言环境的开始/结束分隔符键是否可以未定义。在这种情况下,上面的代码将回退到正常的
双引号

I don't know if the begin/end delimiter key can be undefined for a locale. In that case the above code would fall back to the normal double-quote ".

这篇关于在Swift中添加引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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