私は現在スウィフトドキュメントでこれを参照しています:反復処理し、辞書の配列を変異させるスウィフト3
let wordArray = ["Man","Child","Woman","Dog","Rat","Goose"]
var arrayOfMutatingDictionaries = [[String:Int]]()
var count = 0
while count < 6
{
arrayOfMutatingDictionaries.append([wordArray[count]:1])
count += 1
}
:私は動的辞書の配列を作成するときに、しかし
You can also iterate over a dictionary to access its key-value pairs. Each item in the dictionary is returned as a (key, value) tuple when the dictionary is iterated, and you can decompose the (key, value) tuple’s members as explicitly named constants for use within the body of the for-in loop. Here, the dictionary’s keys are decomposed into a constant called animalName, and the dictionary’s values are decomposed into a constant called legCount.
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {
print("\(animalName)s have \(legCount) legs") }
// ants have 6 legs // spiders have 8 legs // cats have 4 legs
は、そのコードは機能しませんそれが必要として
は、上記のルーチンが成功し、辞書の配列を作成しますが、私はそれを反復処理しようとすると、ドキュメントが表示さのように:
for (wordText, wordCounter) in arrayOfMutatingDictionaries
私はエラーを取得する:式タイプ[[文字列:int]は]私は全然ことを理解していない多くの文脈
ずに曖昧です。
ここでの目標は、可変ディクショナリの可変配列を持つことです。プログラムの途中で、新しいKey-Valueペアを追加したいが、必要に応じて値を増やすこともできます。私はこのコレクションタイプに結婚していませんが、それはうまくいくと思いました。
アイデア?
'arrayOfMutatingDictionaries'は*配列*であり、ディクショナリではありませんので、' for dict in arrayOfMutatingDictionaries {...} 'を使って繰り返します。 - あなたが何を期待していたかわからない。 –
私はvarとして配列を宣言していると予想していましたが、それは変更可能ですが、上記のコードパターンを使用すると不可能です。 'for Dict for arrayOfMutatingDictionaries'を介して反復することによって、辞書の値を変更することはできません。これは私がしたいことです。 –