I am trying to find the index of an entry in a dictionary. My dictionary is as follows:
// Dictionary
var questions: [[String:Any]] = [
[
"quesID": 1000,
"question": "What is the capital of Alabama?",
"answer": "Montgomery",
],
[
"quesID": 1001,
"question": "What is the capital of Alaska?",
"answer": "Juneau",
]
]
I tried using indexOf but it does not work. My code is as follows:
// Find index of dictionary entry with quesID of 1000
let indexOfA = questions.indexOf(1000) // Should return 0
// Find index of dictionary entry with quesID of 1001
let indexOfB = questions.indexOf(1001) // Should return 1
The
indexOf
function takes a parameter closure which determines whether or not the current value is the one that you're looking for. It then returns an integer orNSNotFound
depending on whether the value was found or not.