2015-03-13 15 views
0

我以這種形式與250個陣列大嵌套的數組:斯威夫特的println和維護引號排序

[["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"]] 

這僅僅是一個樣品原來不按字母順序和更大排序。 我需要按字母順序對此數組進行排序,並在我的文件中對已排序的數組進行硬編碼。我想使用排序功能,然後調用println數組並將其複製到我的代碼:

var myArray = ListOfCountries().countries 
    myArray.sort({ $0[2] < $1[2] }) 
    println(myArray) 

那種不正確的,但的println看起來是這樣的:

[[asia, southern central asia, afghanistan, frequent], 
[europe, southern europe, albania, frequent], 
[oceania, polynesia, american samoa, not], 
[europe, southern europe, andorra, not], 
[africa, central africa, angola, frequent], 
[americas, caribbean, anguilla, not]] 

我已經失去了因此我的引號不能在我的代碼和其他需要使用「」的地方複製過去。 我怎麼能做同樣的事情,但用引號訪問排序的數組?

+0

這是因爲它們是字符串。如果你想括號,使用轉義字符:'「\」asia \「」'。 – AstroCB 2015-03-13 14:00:23

+0

是否有引號出現在第一位?我會說只有在日誌中出現。 – 2015-03-13 14:07:09

回答

1

你提到保存報價在你的問題。這讓我認爲你認爲引號是你的字符串的一部分,並且排序將它們刪除了。

但事實並非如此。如果您使用Swift 代碼中的"europe"這樣的字符串,則中的內存中的表示只是europe而不包含引號。

什麼println()寫入到控制檯是一個外部表示的對象。在String值的情況下,它將打印它們而不添加引號。這只是一個實現細節,與字符串如何存儲在應用程序中無關。

你提到你以後需要這個數組和引用的字符串。如果這意味着您需要JSON格式的數據,那麼您將需要使用JSON編碼器,該編碼器將採用您的字符串數組並根據JSON格式化規則將其寫出,其中做的包括引號。

+0

就是這樣:數組的jsonification,然後我可以使用引號將json格式的println打印出來。 Tis – 2015-03-13 14:28:42

1

嘗試:

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) 
+0

也工作也tsk。 – 2015-03-13 14:32:37