In Swift, how to detect duplicates in an encoded custom class array?
<p>I have an array that stores a custom class that has been encoded so it can be saved to NSUserDefaults.  Its based off tableView cells with a favorite button, every time the favorite button is pressed its saved to a local array then saved to NSUserDefaults.  The issue is if the favorite button is pressed multiple times, multiple copies of that tableView cell will be saved to NSUserDefaults. Here is my code to save the data.  I tried checking to see if the "savedFavorite" already exists in the array but since it has been encoded its a different encode value each time so it doesn't look like a duplicate:  </p>

<pre><code> let myDefaults = NSUserDefaults.standardUserDefaults()
  let savedShow1 = "savedShow1"
  var savedFavorite: TvShowInfo!
  var savedFavoriteArray:[TvShowInfo] = []

  @IBAction func saveShow(sender: UIButton) {

    //accessing current point of tableView Cell
    let location: CGPoint = sender.convertPoint(CGPointZero, toView: self.tvShowTableView)
    let indexPath: NSIndexPath = self.tvShowTableView.indexPathForRowAtPoint(location)!
    let searchTermsData = myDefaults.objectForKey(savedShow1) as? NSData

    //accessing previously saved info so as not to overwrite it
    if searchTermsData?.length &gt; 0 {
    let searchTermsArray = NSKeyedUnarchiver.unarchiveObjectWithData(searchTermsData!) as? [TvShowInfo]

    self.savedFavoriteArray = searchTermsArray!
    }

    if searchBarActive {
      savedFavorite = filteredShowSearchResults[indexPath.row]
    } else {
      savedFavorite = showArray[indexPath.row]
    }

      if savedFavoriteArray.contains(savedFavorite) {
        print("Item already in array")
      } else {
        savedFavoriteArray.append(savedFavorite)
   }

    let savedData = NSKeyedArchiver.archivedDataWithRootObject(savedFavoriteArray)
    myDefaults.setObject(savedData, forKey: savedShow1)
    myDefaults.synchronize()
  } 
</code></pre>

<p>Here is my custom class:</p>

<pre><code>class TvShowInfo: NSObject, NSCoding {

  var poster: String
  var title: String
  var id: NSNumber

 init(poster: String, title: String, id: NSNumber) {

    self.poster = poster
    self.title = title
    self.id = id

  }

  // MARK: - comply wiht NSCoding protocol

  func encodeWithCoder(aCoder: NSCoder) {
    aCoder.encodeObject(poster, forKey: "poster")
    aCoder.encodeObject(title, forKey: "title")
    aCoder.encodeObject(id, forKey: "id")
  }

  required convenience init?(coder aDecoder: NSCoder) {

    // decoding could fail, for example when no Blog was saved before calling decode
    guard let unarchivedPoster = aDecoder.decodeObjectForKey("poster") as? String,
          let unarchivedTitle =  aDecoder.decodeObjectForKey("title") as? String,
          let unarchivedId = aDecoder.decodeObjectForKey("id") as? NSNumber

    else {
        // option 1 : return an default Blog
      self.init(poster: "unkown", title:"unkown", id: 0)
      return
    }

    // convenience init must call the designated init
    self.init(poster: unarchivedPoster, title: unarchivedTitle, id: unarchivedId)
  }
}
</code></pre>