iOS SwiftUI-8 Dictionary與Array用法


Posted by John on 2021-01-28

1.Dictionary巢狀使用
2.Array巢狀使用
3.Dictionary與Array混用

  1. Dictionary巢狀使用,key == "name", value == n個dictionary
    目標
    index               [0]            [1]      
                         |              |      
                   - - - - - - -     - - - -   
                   |     |     |     |     |   
    key == "name" [0]   [1]   [2]    [0]  [1]
    
    var dictionary: [[String: Any]] = []
    var content: [[String: String]] = []

     //key == "name", value == 3個dictionary
    content.append(["data1":"123"])
    content.append(["data2":"234"])
    content.append(["data3":"345"])
    dictionary.append(["name": content])

    //key == "name", value == 2個dictionary
    content = []
    content.append(["data4":"456"])
    content.append(["data5":"567"])
    dictionary.append(["name": content])
    if let content1 = dictionary[0]["name"] {
        //[["data1": "123"], ["data2": "234"], ["data3": "345"]]
        content1
    }

    if let content2 = dictionary[1]["name"] {
        //[["data4": "456"], ["data5": "567"]]
        content2
    }
  1. Array巢狀使用
    目標
    index            [0]            [1]      
                      |              |      
                - - - - - - -     - - - -   
                |     |     |     |     |   
               [0]   [1]   [2]    [0]  [1]
    
    var array = Array<Array<Any>>()
    var arrayTemp = Array<String>()

    arrayTemp.append("123")
    arrayTemp.append("234")
    arrayTemp.append("345")
    array.append(arrayTemp)

    arrayTemp = Array<String>()
    arrayTemp.append("456")
    arrayTemp.append("567")
    array.append(arrayTemp)
    //array[0] == ["123", "234", "345"]
    array[0]
    //array[1] == ["456", "567"]
    array[1]
  1. Dictionary與Array混用
    通常用於Dictionary解析到資料,將此資料存入Array做整理

a. 先建立一個Item

    class DataItem {
        var name: String? = ""
        var index: Int? = -1
        //允許兩種初始化樣式
        init(name: String?, index: Int?) {
            self.name = name
            self.index = index
        }
        init() {

        }
    }

b. 先模擬存入Dictionary,在存入list

    var dictionary: [[String: Any]] = []
    var content: [[String: String]] = []
    var list = Array<DataItem>()

     //key == "name", value == 3個dictionary
    content.append(["name":"John"])
    content.append(["index":"100"])
    dictionary.append(["Test1": content])

    //key == "name", value == 2個dictionary
    content = []
    content.append(["name":"Apple"])
    content.append(["index":"59"])
    dictionary.append(["Test2": content])
    if let content1 = dictionary[0]["Test1"] as? [[String: String]] {
        //[["name": "John"], ["index": "100"]]
        let dataItem = DataItem(name: content1[0]["name"], index: Int(content1[0]["index"] ?? "-1"))

        list.append(dataItem)
    }

    if let content2 = dictionary[1]["Test2"] as? [[String: String]] {
        //[["name": "Apple"], ["index": "59"]]
        let dataItem = DataItem(name: content2[0]["name"], index: Int(content2[0]["index"] ?? "-1"))

        list.append(dataItem)
    }
    //list == [{name "John", index -1}, {name "Apple", index -1}]

##Swift #XCode #電腦程式 #程式設計 #基礎語法







Related Posts

MTR04_0921

MTR04_0921

Command Line

Command Line

第一章 - 你好, 世界!

第一章 - 你好, 世界!


Comments