Swift Println并在数组排序后保留引号
我有一个很大的嵌套数组,其中有250个数组,格式如下:Swift Println并在数组排序后保留引号,swift,Swift,我有一个很大的嵌套数组,其中有250个数组,格式如下: [["europe", "eastern europe", "belarus", "frequent"], ["europe", "eastern europe", "bulgaria", "frequent"], ["europe", "eastern europe", "czech republic", "frequent"], ["europe", "eastern europe", "hungary", "f
[["europe", "eastern europe", "belarus", "frequent"],
["europe", "eastern europe", "bulgaria", "frequent"],
["europe", "eastern europe", "czech republic", "frequent"],
["europe", "eastern europe", "hungary", "frequent"],
["europe", "eastern europe", "moldova", "not"],
["europe", "eastern europe", "romania", "frequent"],
["europe", "eastern europe", "slovakia", "frequent"]]
这只是一个样本,原始样本没有按字母顺序排序,而且要大得多。
我需要按字母顺序对数组进行排序,并在我的文件中硬编码排序后的数组。我想使用sort函数,然后打印数组并将其复制到我的代码中:
var myArray = ListOfCountries().countries
myArray.sort({ $0[2] < $1[2] })
println(myArray)
我丢失了引号,因此我无法在我的代码和其他需要使用“”的地方复制它。
我怎样才能做同样的事情,但却能够访问带引号的排序数组?您在问题中提到了保留引号。这让我觉得引号是字符串的一部分,排序删除了它们
但事实并非如此。如果在Swift代码中使用类似于“europe”
的字符串,那么内存中的表示形式就是不带引号的europe
println()
写入控制台的是对象的外部表示。如果是String
值,它将在不添加引号的情况下打印它们。这只是一个实现细节,与应用程序中字符串的存储方式无关
您提到,稍后需要使用带引号的字符串的数组。如果这意味着您需要使用JSON
格式的数据,那么您需要使用JSON编码器,它将获取字符串数组,并根据JSON格式规则将其写出,其中包括引号。尝试:
debugPrintln(myArray)
它用“
引用字符串
///向控制台写入最合适的“x”的文本表示形式
///用于调试,后跟换行符。
///
///…剪断。。。
///
///另见:`debugPrint(x)`
func debugPrintln(x:T)
这是因为它们是字符串。如果需要括号,请使用转义括号:“\“asia\”
。引号是否首先出现在那里?我想说唯一出现在日志中的是:数组的jsonification,然后我可以用json格式打印LN,带引号。Tis
debugPrintln(myArray)
/// Write to the console the textual representation of `x` most suitable
/// for debugging, followed by a newline.
///
/// ... snip ...
///
/// See also: `debugPrint(x)`
func debugPrintln<T>(x: T)