Swift:打印数组值按添加排序

时间:2018-09-15 09:09:58

标签: swift

我的代码:

internal static var myList = [Int: String]()

let data = "1:Tea#12:Food#2:Milk#7:Juice#4:Coffee"
let array = data.components(separatedBy: "#")
for value in array {
    let val = value.components(separatedBy: ":")
    myList[Int(val[0])!] = "\(val[1])"
}

现在打印数组:

var j = 0
for sort in myList {
    print("\(sort.value) (+\(sort.key))")
}

输出:

Tea
Milk
Coffee
Juice
Food

但是我想通过添加到数组来打印:

Tea
Food
Milk
Juice
Coffee

我的数组按键打印值,但我想通过添加到数组来打印

1 个答案:

答案 0 :(得分:0)

使用结构

<DataTemplate>
    <ViewCell>
       <StackLayout>
            <Label Text="{Binding strArticleTitle}" />
            <Label Text="{Binding strArticleURL}" />
        </StackLayout>
    </ViewCell>
</DataTemplate>

并将字符串组件的数组映射到该结构的实例

struct Item {
    let added : Int
    let index : Int
    let name : String
}