Swift:将2D数组转换为自定义对象的数组

时间:2019-11-29 08:09:27

标签: arrays swift dictionary

let languages = ["en", "ru", "pt"]

let sheetValues = [["key", "general_ok", "connection_nointernet_message"], ["en", "Ok", "No internet connection"], ["ru", "russianOk", "No internet connection"], ["pt", "portugueseOk", "No internet connection"], ["description", "Ok", "No internet connection"], ["section", "General", "Connection"]]

我可以操纵sheetValues进行转置(如果需要)

我想要的是下面的

[
    [
        "isoCode": "en",
        "data": [SheetData("key": "general_ok", "value": "Ok", "description": "Ok", "section": "General"),
                 SheetData("key": "connection_nointernet_message", "value": "No internet connection", "description": "No internet connection", "section": "Conenction")]

    ],
    [
        "isoCode": "ru",
        "data": [SheetData("key": "general_ok", "value": "russianOk", "description": "Ok", "section": "General"),
                 SheetData("key": "connection_nointernet_message", "value": "No internet connection", "description": "No internet connection", "section": "Conenction")]

    ],
    [
        "isoCode": "pt",
        "data": [SheetData("key": "general_ok", "value": "portugueseOk", "description": "Ok", "section": "General"),
                 SheetData("key": "connection_nointernet_message", "value": "No internet connection", "description": "No internet connection", "section": "Conenction")]
    ]
]

型号:

struct Language {
    var isoCode: String?
    var data: [SheetData]?
}

struct SheetData {
    let key: String
    let value: String
    var description: String?
    let section: String
}

这是我目前拥有的任何干净的方式来获取所需的东西(即[SheetData])?

func fetchLanguages() -> [Language] {
    var allLanguages = [Language]()

    for sheetValue in sheetValues {
        var language = Language()
        var allDataPoints = [SheetData]()

        if let firstElement = sheetValue.first {
            if languages.contains(firstElement) {
                language.isoCode = firstElement
            }
        }

        allLanguages.append(language)
    }

    return allLanguages
}

1 个答案:

答案 0 :(得分:0)

好的,我查看了您的数据并提出了解决方案。末尾的result值应具有所需的数组。

        // First create a dictionary with keys for each language
        // In case new languages added or removed
        let languages = ["en", "ru", "pt"]
        var datas: [String: [String: Any]] = [:]
        for language in languages {
            var sheetData: [String: Any] = [:]
            sheetData["isoCode"] = language
            sheetData["data"] = []

            datas[language] = sheetData
        }

        // Get your sheetValues from your source. Here I just used the array you provided.
        let sheetValues = [["key", "general_ok", "connection_nointernet_message"],
                           ["en", "Ok", "No internet connection"],
                           ["ru", "russianOk", "No internet connection"],
                           ["pt", "portugueseOk", "No internet connection"],
                           ["description", "Ok", "No internet connection"],
                           ["section", "General", "Connection"]]

        // Loop the sheetValues starting from index 1 since index 0 holds the key values. 
        for i in 1..<sheetValues.count {
            let languageKey = sheetValues[i][0]
            if datas.keys.contains(languageKey) {
                let firstSheetData = SheetData(key: sheetValues[0][1], // general_ok
                                               value: sheetValues[i][1], // Ok, russianOk etc
                                               description: sheetValues[sheetValues.count - 2][1], // Ok
                                               section: sheetValues[sheetValues.count - 1][1]) // General

                let secondSheetData = SheetData(key: sheetValues[0][2], // connection_nointernet_message
                                                value: sheetValues[i][2], // No internet connection
                                                description: sheetValues[sheetValues.count - 2][2], // No internet connection
                                                section: sheetValues[sheetValues.count - 1][2]) // Connection

                datas[languageKey]["data"] = [firstSheetData, secondSheetData]
            }
        }

        // Loop the dictioanary holding the language datas
        var result: [[String: Any]] = []
        for (key, value) in datas {
            result.append(value)
        }

此解决方案仅在某些条件下适用:

  • sheetValues的索引0将始终具有key的值
  • sheetValues中倒数第二个索引将始终具有description
  • 最后一个索引sheetValues将始终具有section