4

I am trying to access the following items from an Array of dictionaries and I have two problems (both are different approaches). The array of dictionaries is initialized as follows:

var testingArray = [[String: String]()]

testingArray.append(["name": "Ethiopia", "url": "localhost:8088"])
testingArray.append(["name": "Bugatti", "url": "localhost:8088"])
testingArray.append(["name": "Brazil", "url": "localhost:8088"])
testingArray.append(["name": "Jasmine", "url": "localhost:8088"])
testingArray.append(["name": "Hello", "url": "localhost:8088"])

The first method:

for (k,v) in testingArray {
    // code here
}

Won't run due to (which appears on the line the for loop is initialized):

"Expression type '[[String : String]]' is ambiguous without more context

The second method:

for indices in testingArray {
    for(k, v) in indices {
        print(indices.keys)
    }
}

returns the following:

LazyMapCollection<Dictionary<String, String>, String>(_base: ["url": "localhost:8088", "name": "Ethiopia"], _transform: (Function))

LazyMapCollection<Dictionary<String, String>, String>(_base: ["url": "localhost:8088", "name": "Ethiopia"], _transform: (Function))

LazyMapCollection<Dictionary<String, String>, String>(_base: ["url": "localhost:8088", "name": "Bugatti"], _transform: (Function))

LazyMapCollection<Dictionary<String, String>, String>(_base: ["url": "localhost:8088", "name": "Bugatti"], _transform: (Function))

LazyMapCollection<Dictionary<String, String>, String>(_base: ["url": "localhost:8088", "name": "Brazil"], _transform: (Function))

LazyMapCollection<Dictionary<String, String>, String>(_base: ["url": "localhost:8088", "name": "Brazil"], _transform: (Function))

LazyMapCollection<Dictionary<String, String>, String>(_base: ["url": "localhost:8088", "name": "Jasmine"], _transform: (Function))

LazyMapCollection<Dictionary<String, String>, String>(_base: ["url": "localhost:8088", "name": "Jasmine"], _transform: (Function))

LazyMapCollection<Dictionary<String, String>, String>(_base: ["url": "localhost:8088", "name": "Hello"], _transform: (Function))

LazyMapCollection<Dictionary<String, String>, String>(_base: ["url": "localhost:8088", "name": "Hello"], _transform: (Function))

Here is pseudocode equivalent that I am trying to achieve:

for(int i = 0; i < sizeOfArray; i++ {
    print testingArray[i]."name"
    print testingArray[i]."url"
}

I have been scratching my head over this for days but I don't know swift and its idioms well enough to solve this alone, any help would greatly be appreciated (esp if we can figure out how to get #1 working).

1
  • In order to iterate a swift array with indices, see stackoverflow.com/a/24028458/958064 - Swift 3.0 adds an enumerated() method that would help OP here Commented Dec 14, 2017 at 20:12

2 Answers 2

7

I agree the error message is confusing/misleading. But for (k,v) in testingArray doesn't make sense, because testingArray is an array, not a dictionary. Its elements are dictionaries.

I think you're looking for something like this:

for obj in testingArray {
    print(obj["name"])
    print(obj["url"])
}
Sign up to request clarification or add additional context in comments.

4 Comments

thanks! It works like a charm. This is my first time using dictionaries so having a good grasp of the right syntax the first time is really important to me. It works, also just a side question, since dictionaries are unordered and non-enumerable, if I want to load from plist to a PickerView the easiest approach is to store all my dictionary kv pairs in an array yes?
If you need them to be in a specific order, yes, it'll be easiest to have them in an array. If each thing contains multiple properties (e.g. a "display name" and an "identifier") then it makes sense to use an array of dictionaries, as you've done in your example. (However, dictionaries are enumerable: in my example, you could do an inner loop like for (k, v) in obj { ... })
when you say enumerable, does that mean I can get the values from a dictionary within this function: pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {} like so: dictionary[row]["name"] ?
A dictionary itself is not indexable by integer row numbers. If you have an array of dictionaries called array, then you could do array[row]["name"], yes.
3

this works

for (k,v) in testingArray.enumerated() {
    for (_, element) in k.enumerated(){
       guard let elem = element.value as? [String: Any] else {
            continue
       }
       print elem["name"]
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.