title: RealmSwift - How to override hash function when migrating pre-existing Class to Realm
body: <p>I have an existing <code>Movie</code> class that I now wish to store using the <code>RealmSwift</code> Framework. Previously I had conformed <code>Movie</code> to the <code>Hashable</code> protocol (for performing <code>Set()</code> operations on arrays of Movies), but this is made redundant by <code>Realm</code>'s requirement to conform to its <code>Object</code> protocol.</p>
  
  <p>However, now I get an error with my (re)declaration of the <code>hash(into hasher: _)</code> function (as needed by <code>Hashable</code>):</p>
  
  <pre><code>Overriding declarations in extensions is not supported
  Overriding non-open instance method outside of its defining module
  1. Overridden declaration is here (ObjectiveC.NSObject)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/zKn4g.png" rel="noreferrer"><img src="https://i.stack.imgur.com/zKn4g.png" alt="Screenshot of the error messages from declaring func hash(...), as previously required to conform to Hashable"></a></p>
  
  <hr>
  
  <p>I've checked many issue reports on the <code>Realm</code> git repo to no success (and some have been incorrectly marked as closed, I believe):</p>
  
  <ul>
  <li><p><a href="https://github.com/realm/realm-cocoa/issues/4705" rel="noreferrer">Conformance to Hashable is broken for objects without primary key</a></p></li>
  <li><p><a href="https://github.com/realm/realm-cocoa/issues/5927" rel="noreferrer">Error when implementing Hashable in Xcode 10</a></p></li>
  <li><p><a href="https://github.com/realm/realm-java/issues/909" rel="noreferrer">Support adding own methods to objects</a></p></li>
  </ul>
  
  <p>I've tried with and without a declared <code>primaryKey</code>, as well as putting a dummy initial (guaranteed unique) id via <code>dynamic var id = UUID().uuidString</code> - although it should be noted that <code>Movie()</code> is <strong><em>NEVER</em></strong> called - I always use the (now convenience, with this Realm refactor) <code>convenience init(title: String, id: String, cast: String )</code> initialiser. </p>
  
  <p>Some suggestions include overriding a <code>hash</code> or <code>hashValue</code> variable, but the latter has been made redundant by the former (and the former still doesn't work for me - in this example case, I am just using the <code>id</code> parameter for the <code>Movie</code>'s <code>hash/hashValue</code>:</p>
  
  <pre><code>override var hash: Int {
    return id.hashValue
  }
  
  override var hashValue: Int {
    return id.hashValue
  }
  </code></pre>
  
  <hr>
  
  <p>My <code>Movie</code> code is as follows (with the fatal error on <code>func hash(into hasher: inout Hasher) {</code> as shown in the above screenshot):</p>
  
  <pre><code>import Foundation
  import UIKit
  import EventKit
  import RealmSwift
  
  @objcMembers class Movie: Object {
   dynamic var title: String = ""
   dynamic var id: String = ""
   dynamic var cast: String  = ""
  
   convenience init(title: String, id: String, cast: String ) {
      self.init()
      self.title = title
      self.id = id
      self.cast = cast
   }
  
   override static func primaryKey() -&gt; String? {
      return "id"
   }
  
   func hash(into hasher: inout Hasher) {
      hasher.combine(title)
      hasher.combine(id)
      hasher.combine(cast)
   }
  }
  </code></pre>
  
  <hr>
  
  <p>An example of the <code>Set()</code> arithmetic that I wish to perform on arrays of Movies, <code>[Movie]</code>. In this example I am checking if <code>subSetOfMovies</code> is a sub-set of <code>allMovies</code>, <em>if-not</em> then add it to the list of <code>allMovies</code>:</p>
  
  <pre><code> if !Set&lt;Movie&gt;(subSetOfMovies).isSubset(of: Set(allMovies)) {
     allMovies.append(contentsOf: subSetOfMovies)
   } else {
     print("subSetOfMovies is a subset of allMovies, thus it is not added.")
   }
  </code></pre>
  
  <p>Note: I wish to use Set arithmetic because it is very fast. For long lists of movies I do not wish to check that each movie, individually (say with a for-loop), is <em>not</em> included in the <code>allMovies</code> array.</p>
  
  <hr>
  
  <p>Basically, I want to be able to manually reconform my <code>Movie</code> class to <code>Hashable</code> so that I can perform set arithmetic on it (as I did before I started to implement <code>RealmSwift</code> to my <code>Movie</code> Model). However, as things currently stand, it seems that I can't with <code>Realm</code> - unless I'm missing something obvious...</p>