title: Make Realm Loop Generic and Reusable in Swift
body: <p><em>Swift 4, Xcode 9.2</em></p>
  
  <p>Here are a few Realm classes I have:</p>
  
  <pre><code>class Dog: Object {
    @objc dynamic var id = UUID().uuidString
    @objc dynamic var updated = Date()
    //Other properties...
  }
  
  class Cat: Object {
    @objc dynamic var id = UUID().uuidString
    @objc dynamic var updated = Date()
    //Other properties...
  }
  
  class Horse: Object {
    @objc dynamic var id = UUID().uuidString
    @objc dynamic var updated = Date()
    //Other properties...
  }
  </code></pre>
  
  <p>Let's say I have some code like this where I compare two realms and create an object with a specific <code>Dog</code> class:</p>
  
  <pre><code>let remoteDogs = remoteRealm.objects(Dog.self)
  
  for remoteDog in remoteDogs{
    if let localDog = realm.objects(Dog.self).filter("id = %@",remoteDog.id).first{
      // Update
      if localDog.updated &lt; remoteDog.updated{
        //Remote is newer; replace local with it
        realm.create(Dog.self, value: remoteDog, update:true)
      }
    }
  }
  </code></pre>
  
  <p>This works great, but I need to do this same stuff on a whole bunch of Realm classes that I have. So I'm trying to make it more generic like this:</p>
  
  <pre><code>let animals = [Dog.self, Cat.self, Horse.self]
  
  for animal in animals{
    let remoteAnimals = remoteRealm.objects(animal)
  
    for remoteAnimal in remoteAnimals{
      if let localAnimal = realm.objects(animal).filter("id = %@",remoteAnimal.id).first{
        // Update
        if localAnimal.updated &lt; remoteAnimal.updated{
          //Remote is newer; replace local with it
          realm.create(animal, value: remoteAnimal, update:true)
        }
      }
    }
  }
  </code></pre>
  
  <p>This sort of works, but anytime I want to reference the property of an object (like with <code>remoteAnimal.id</code> and <code>remoteAnimal.updated</code>) then the compiler complains because it doesn't know what kind of object a <code>remoteAnimal</code> is.</p>
  
  <p>Has anyone done something like this before? Any ideas how I can do this so that I don't have to write this same code over and over for each of my Realm classes? Thanks!</p>