title: Swift 4 Key Value Dictionary stored in User Defaults
body: <p>I am new to Swift and have a requirement to store a database of key value pairs. The key value pairs are a name with a corresponding 4 digit number in database that remains in memory after the app is excited. I am thinking to use a dictionary with the name as the key and the 4 digit numbers as the value. These are then stored in the iPad flash memory using the user defaults class. </p>
  
  <p>Below is the code that I’ve currently  developed. The code that adds to the database compiles ok but the code that checks the name and number for a match in the database won't compile due to the following message (Value of optional type '[Any]?' not unwrapped; did you mean to use '!' or '?'?) which is because of this line of code (if let databaseCheck = database[name]). Ive obviously tried unwrapping but can't seem to shake the error message.</p>
  
  <p>Anyone got any ideas whats causing the error or any issues with the approach?</p>
  
  <pre><code>public func checkDatabaseMatch( _ name: String, _ number: String) -&gt; Bool
  {
     var foundInDatabaseFlag: Bool = false
  
     let database = UserDefaults.standard.array(forKey: "Database")
  
     if let databaseCheck = database[name]
     {
        if (databaseCheck == number)
        {
           foundInDatabaseFlag = true
        }
     }
  
     return foundInDatabaseFlag
   }
  
  public func saveToDatabase( _ name: String, _ number: String)
  {
     var newEntry: [String: String] = [:]
  
     newEntry[name] = number
  
     UserDefaults.standard.set(newEntry, forKey: "Database")
  }
  </code></pre>