16. Recreate a Swift Dictionary

If asked in a technical interview, could you recreate the main functionally used for a native Swift Dictionary? Popular in iOS development, Dictionaries are particular collection types that associate values to a key.

 let vehicles = Glossary<String, String>()
        
 //add new values
 vehicles.updateValue("Porshe", forKey: "Car")
        
 //check existing value
 if let results = vehicles.valueForKey("Car") {
    print("car is: \(results)")
 }

In this example, we’ve designed a custom Dictionary with the type name Glossary. As shown, we can use the custom Glossary to associate and retrieve a value for a specified key (e.g., Car). 

Hints

  • What structure(s) will you use to store dictionary keys and values?

  • How will you associate a key to a value?

  • How will your solution identify duplicate keys?

  • How will your algorithm obtain all keys as well as values independently?

  • Will your custom type support Generics? How will this work?

  • What actions occur when attempting to delete dictionary records?