Swift字典:Can';不要完全删除条目

Swift字典:Can';不要完全删除条目,swift,swift-playground,swift-dictionary,Swift,Swift Playground,Swift Dictionary,我有一本Swift字典,我正试图完全删除一个条目。我的代码如下: import UIKit var questions: [[String:Any]] = [ [ "question": "What is the capital of Alabama?", "answer": "Montgomery" ], [ "question": "What is the capital of Alaska?", "a

我有一本Swift字典,我正试图完全删除一个条目。我的代码如下:

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?"

我使用问题[0].removeAll()删除该条目,但它会留下一个空条目。如何才能完全删除一个条目,使其不存在任何跟踪?

此行为没有任何问题,您正在告诉编译器,它删除了
字典中的所有元素,并且工作正常:

questions[0].removeAll()
但是,您正在声明一个
数组
或使用简写语法
[[String:Any]]]
,如果要删除
字典
,也需要从数组中删除条目,请参阅以下代码:

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()

questions.removeAtIndex(0) // removes the entry from the array in position 0

ask1 = questions[0] // ["answer": "Juneau", "question": "What is the capital of Alaska?"]
ask2 = ask1["question"] // "What is the capital of Alaska?"

我希望这对您有所帮助。

ask1=questions[0]
不在代码末尾的第二行。
[:]。@MartinR是的,您是对的,我的错误我忘了在操场上更新值,谢谢,修复了