I have a Swift dictionary and I am trying to completely remove an entry. My code is as follows:
import UIKit
var questions: [[String:Any]] = [
[
"question": "What is the capital of Alabama?",
"answer": "Montgomery"
],
[
"question": "What is the capital of Alaska?",
"answer": "Juneau"
]
]
var ask1 = questions[0]
var ask2 = ask1["question"]
print(ask2!) // What is the capital of Alabama?
questions[0].removeAll()
ask1 = questions[0] // [:]
ask2 = ask1["question"] // nil - Should be "What is the capital of Alaska?"
I used questions[0].removeAll() to remove the entry but it leaves an empty entry. How can I completely remove an entry so that there is no trace?
There is nothing wrong with this behaviour, you are telling to the compiler that removes all the elements in a
Dictionary
and it's working fine:But you're declaring an
Array<Dictionary<String, Any>>
or in shorthand syntax[[String: Any]]
and you need to remove the entry from your array too if you want to remove theDictionary
, see the following code:I hope this help you.