1 Answer

You can use subscript syntax to remove a key-value pair from a dictionary by assigning a value of nil for that key:

airports["APL"] = "Apple International"

// "Apple International" is not the real airport for APL, so delete it

airports["APL"] = nil

// APL has now been removed from the dictionary

Alternatively, remove a key-value pair from a dictionary with the removeValueForKey method. This method removes the key-value pair if it exists and returns the removed value, or returns nil if no value existed:

if let removedValue = airports.removeValueForKey("DUB") {

println("The removed airport's name is \(removedValue).")

} else {

println("The airports dictionary does not contain a value for DUB.")

}

// prints "The removed airport's name is Dublin Airport."

Your comment on this answer:

Your name to display (optional):

Email me at this address if a comment is added after mine:Email me if a comment is added after mine

Privacy: Your email address will only be used for sending these notifications.