您只需使用它的String
鍵刪除該項目。
images.removeValueForKey("someString")
編輯: 每點評:
我不完全明白你的意思。我將重新創建你的場景。但我會使用Strings
而不是UIImage's
,因爲我在命令行上執行此操作。
1> var images = [String:String]()
images: [String : String] = 0 key/value pairs
2> images["one"] = "someting 1 2 3"
3> images["two"] = "something else 4 5 6"
4> images
$R0: [String : String] = 2 key/value pairs {
[0] = {
key = "one"
value = "someting 1 2 3"
}
[1] = {
key = "two"
value = "something else 4 5 6"
}
}
現在,根據您的示例,我想刪除一個項目。
5> images.removeValueForKey("one")
$R1: String? = "someting 1 2 3"
6> images
$R2: [String : String] = 1 key/value pair {
[0] = {
key = "two"
value = "something else 4 5 6"
}
}
EDIT2:
看完後你讚揚你的示例代碼第二次,我想你是誤會你有什麼。
你有一個字符串鍵的字典。與UIImages以一對一的關係配對。
所以,如果你刪除一個項目的基礎上的關鍵,你刪除它的唯一屬性是一個圖像。
現在,由於您在原始問題中提到了數組,所以我認爲這就是您想要的。
1> var images = [String:[String]]()
對您而言,這將是`變種圖像=字符串:[UIImage的]
現在你有每個鍵圖像的陣列。
images: [String : [String]] = 0 key/value pairs
1> images["one"] = [String]
2> images["one"]!.append("first")
3> images
$R0: [String : [String]] = 1 key/value pair {
[0] = {
key = "one"
value = 1 value {
[0] = "first"
}
}
}
4> images["one"]!.append("second")
5> images
$R1: [String : [String]] = 1 key/value pair {
[0] = {
key = "one"
value = 2 values {
[0] = "first"
[1] = "second"
}
}
}
6> images["one"]!.removeAtIndex(0)
$R2: String = "first"
7> images
$R3: [String : [String]] = 1 key/value pair {
[0] = {
key = "one"
value = 1 value {
[0] = "second"
}
}
}
字典是無序的,所以這個功能可能會給予回覆意想不到的結果。 – tktsubota
我看到swift有這個選項:removeAtIndex(index:DictionaryIndex)。但我不知道如何使用它。 –
laser2302
在這種情況下,我相信DictionaryIndex只是字符串的鍵入類型,即字典的鍵類型。 –