我正在處理一些代碼并且收到了不正確的輸出并一直在嘗試解決它,我知道它是一個非常簡單的修復(或者至少應該是)可能正確地轉換它或處理它。但是,我進行了多次嘗試,但均未成功。
這是 Swift Playground 中的代碼
import UIKit
import Foundation
// MARK:- Declarations
// The varible defintions including the expected unique key values, the associated data and the dictionary which will be returned
var arrayOfUniqueFieldNames = ["value1", "value2", "value3"]
var dictionaryOfKeyPairedData : [Int: [String]] = [
0: ["value1", "a"],
1: ["value2", "b"],
2: ["value3", "c"],
3: ["value1", "x"],
4: ["value2", "y"],
5: ["value3", "z"]]
var dictionaryOfDatasets = [String: Any]()
// MARK:- Code Body
// Iterates through the dictionaryOfKeyPairedData assigning each key if it doesn't exist to the array to print and the value to the existing key, each key has mutliple values stored as an array
for counterOne in 0...dictionaryOfKeyPairedData.count-1{
// gets the current key value from the dictionary as a String
let currentKeyValue = returnKeyofDictionaryArray(theDictionaryInQuestion: dictionaryOfKeyPairedData, indexToLookUp: counterOne, value: 0)
// gets the current value from the dictionary as a String as it associates with the current key
let currentValuePair = returnKeyofDictionaryArray(theDictionaryInQuestion: dictionaryOfKeyPairedData, indexToLookUp: counterOne, value: 1)
// Fetches the array from the dictionary so it can be manipulated
var existingItems = Array(arrayLiteral: dictionaryOfDatasets[currentKeyValue]) //Where I believe the Optional is not handled correctly
// If the key doesn't exist it saves the value as the key to the key, if it does exist it saves the value to the value as a value
if existingItems[0] == nil{
existingItems = [currentValuePair] //Where I believe the extranious brackets are getting added
}else{
existingItems.append(currentValuePair)
}
// resaves the manipulated array alongside the existing key
dictionaryOfDatasets[currentKeyValue] = existingItems
}
// prints the data - OUTPUT
print(dictionaryOfDatasets)
// MARK:- Functions
// Returns the key for the value for the prescribed array
func returnKeyofDictionaryArray(theDictionaryInQuestion: [Int: [String]], indexToLookUp: Int, value: Int) -> String{
return theDictionaryInQuestion[indexToLookUp]![value]
}
預期的輸出是
["value2": ["b", "y"], "value1": ["a", "x"], "value3": ["c","z"]]
要么
["value1": ["a", "x"], "value2": ["b", "y"], "value3": ["c","z"]]
實際輸出是
["value1": [Optional([Optional("a")]), Optional("x")], "value2": [Optional([Optional("b")]), Optional("y")], "value3": [Optional([Optional("c")]), Optional("z")]]
uj5u.com熱心網友回復:
您正在使用Any作為輸出字典值的型別,而它可以只是一個Strings 陣列。并且使用Array(arrayLiteral: dictionaryOfDatasets[currentKeyValue])沒有幫助。
您可以通過遍歷原始字典并將值直接存盤到新字典中來獲得所需的結果,而無需像returnKeyofDictionaryArray.
以下是如何實作這一目標:
// The variable defintions including the expected unique key values, the associated data and the dictionary which will be returned
var arrayOfUniqueFieldNames = ["value1", "value2", "value3"]
var dictionaryOfKeyPairedData : [Int: [String]] = [
0: ["value1", "a"],
1: ["value2", "b"],
2: ["value3", "c"],
3: ["value1", "x"],
4: ["value2", "y"],
5: ["value3", "z"]]
// This is a dictionary of String to String-arrays
var dictionaryOfDatasets = [String: [String]]()
// MARK:- Code Body
// Just iterate through the values of the dictionary, given that
// you are not interested in the Int keys
dictionaryOfKeyPairedData.values.forEach {
if dictionaryOfDatasets[$0[0]] == nil {
dictionaryOfDatasets[$0[0]] = [$0[1]]
} else {
dictionaryOfDatasets[$0[0]]?.append($0[1])
}
}
// Sorting, if you need:
// Here is where you sort the arrays inside each value of the dictionary
dictionaryOfDatasets.keys.forEach {
dictionaryOfDatasets[$0] = dictionaryOfDatasets[$0]?.sorted(by: { $0 < $1 })
}
// prints the data - OUTPUT
print(dictionaryOfDatasets) // ["value3": ["c", "z"], "value2": ["b", "y"], "value1": ["a", "x"]]
uj5u.com熱心網友回復:
你,首先你需要用更優雅的方式改變Dictioanry密鑰Any,String你可以用更優雅的方式達到預期的結果
var arrayOfUniqueFieldNames = ["value1", "value2", "value3"]
var dictionaryOfKeyPairedData : [Int: [String]] = [
0: ["value1", "a"],
1: ["value2", "b"],
2: ["value3", "c"],
3: ["value1", "x"],
4: ["value2", "y"],
5: ["value3", "z"]]
var dictionaryOfDatasets = [String: [String]]()
arrayOfUniqueFieldNames.forEach{
let key = $0
dictionaryOfKeyPairedData.filter({$0.value.first == key}).forEach{
if dictionaryOfDatasets[key] != nil {
var val = dictionaryOfDatasets[key]
val?.append($0.value.last ?? "")
dictionaryOfDatasets[key] = val
}else{
dictionaryOfDatasets[key] = [$0.value.last ?? ""]
}
}
}
print(dictionaryOfDatasets)
輸出:
["value1": ["a", "x"], "value3": ["z", "c"], "value2": ["y", "b"]]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/446945.html
