如果可能的话,我想从字典中删除一个键值对。
var dict: Dictionary<String,String> = [:]
var willRemoveKey = "SomeKey"
dict.removePair(willRemoveKey) //that's what I need
答案 0 :(得分:115)
您可以使用:
dict[willRemoveKey] = nil
或者这个:
dict.removeValueForKey(willRemoveKey)
唯一的区别是第二个将返回删除的值(如果它不存在则为零)
dict.removeValue(forKey: willRemoveKey)
答案 1 :(得分:14)
Swift 5 , Swift 4 , Swift 3 :
x.removeValue(forKey: "MyUndesiredKey")
干杯
答案 2 :(得分:5)
dict.removeValueForKey(willRemoveKey)
或者您可以使用下标语法:
dict[willRemoveKey] = nil
答案 3 :(得分:0)
通过“检查条件”中的条件删除
DatabaseReference mArticalReference; mArticalReference=FirebaseDatabase.getInstance().getReference().child("ARTICALE"); String selectedKey="-Lt0JLmniHevNn4JNP57" //replace with your selected key mArticalReference.child(selectedKey)addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { Map<String, String> map = (Map) dataSnapshot.getValue(); if (map != null) { String message = map.get("content"); String userName = map.get("tag1"); } } @Override public void onCancelled(DatabaseError databaseError) { } });
var eventDateDict = [String:String]()
//Declarations
var tempDict = eventDateDict