2016-04-19 141 views
0

我需要你的幫助。我做了一個數組,當我在不同的設備或模擬器上啓動我的應用程序時,這個數組分爲不同的類型,但我需要在每個設備上的順序相同。怎麼做?如何在swift中對多維數組進行排序?

var settings = [String: [ [String: String] ]]() 
    settings = [ "cards":[ ], "groups":[ ] ] 

    ... 

    for card in db.prepare(table.order(orderN.asc)) { 
     settings["cards"]?.append(["id":String(card[id]), "group_id":String(card[group_id]), "service":card[service], "bgcolor":card[bgcolor], "logoimg":card[logoimg]!, "balance_desc":card[balance_desc], "balance":"0.0", "balance_id":String(card[balance_id]), "uniqueID":card[uniqueID], "balance_currency":String(card[balance_currency]), "orderN":String(card[orderN])]) 
    } 

    ... 

    for group in db.prepare(table.order(orderN.asc)) { 
     settings["groups"]?.append(["name":group[name]!,"id":String(group[id]),"orderN":String(group[orderN])]) 
    } 

    ... 

例如, 在第一設備

print(settings) // ["cards": [["orderN": "0", "bgcolor": "0.0, 0.0, 0.0, 1.0", "balance": "0.0", "logoimg": "example.com/images/img.png", "uniqueID": "00a2413f74f4a3f186e439a67057de67", "group_id": "2", "id": "1", "service": "servicename", "balance_desc": "Description", "balance_id": "1", "balance_currency": "1"]], "groups": [["orderN": "0", "name": "GroupName", "id": "2"]]] 

在第二設備

print(settings) // ["cards": [["orderN": "0", "uniqueID": "00a2413f74f4a3f186e439a67057de67", "service": "servicename", "id": "1", "bgcolor": "0.0, 0.0, 0.0, 1.0", "balance_currency": "1", "balance": "0.0", "group_id": "2", "logoimg": "example.com/images/img.png", "balance_id": "1", "balance_desc": "Description"]], "groups": [["id": "2", "orderN": "0", "name": "GroupName"]]] 

謝謝您的關注。

+0

到目前爲止你一直在努力嘗試什麼? – Lasse

回答

0

這是因爲settings不是Array,它是Dictionary。沒有定義字典中的鍵值對的順序。

如果你需要一些特定的順序,你應該重新實現settings,可能使它們分開structclass,因爲你有一個很難嵌套的字典工作。

+0

我需要在所有設備上具有相同的順序,其他事情並不重要。 –

+0

好的,你可以得到你的字典的鍵和排序。例如,'settings [「cards」] [0] .keys.sort()'。然後遍歷這些鍵。 –

相關問題