title: Save Dictionary of custom objects in Swift using UserDefaults
body: <p>I'm writing an app to work with contacts and trying to save information with UserDefaults but I'm getting SIGABRT. Here's my code, what I'm doing wrong?</p>
  
  <pre><code>class Contact{
  var name = String()
  var phone = String()
  
  init(name: String, phone: String){
      self.name=name
      self.phone=phone
  }
  required init(coder decoder: NSCoder){
      self.name = (decoder.decodeObject(forKey: "name") as? String)!
      self.phone = (decoder.decodeObject(forKey: "phone") as? String)!
  }
  func encode(with coder: NSCoder){
      coder.encode(name, forKey: "name")
      coder.encode(phone, forKey: "phone")
  }
  }
  </code></pre>
  
  <p>Creating contact in ViewDidLoad just for test:</p>
  
  <pre><code>        let userDefaults = UserDefaults.standard
  var contactDictionary = Dictionary&lt;String, [Contact]&gt;()
  
    override func viewDidLoad() {
      super.viewDidLoad()
      contactDictionary["A"] = [Contact(name: "Annabel",phone: "000")]
  
      let encodedData = NSKeyedArchiver.archivedData(withRootObject: contactDictionary)
      userDefaults.set(encodedData, forKey: "contactDictionary")
      userDefaults.synchronize()
      if let data = userDefaults.data(forKey: "contactDictionary"){
          print("yep")
          contactDictionary = (NSKeyedUnarchiver.unarchiveObject(with: data) as? [String : [Contact]])!
      }
      else{
          print("nope")
      }
  }
  </code></pre>