Search code examples
iosswiftrealm

Swift 3 - How to cast Real Result<Object> to generic type [T]


I have the following Realm function to fetch all elements from the table in my Swift 3 app.

//Find all elements in the database
func findAll() -> [T] {
   return getRealm().objects(T.self as! Object.Type)
}

How do I cast Result<Object> from Realm to [T] in the return?


Solution

  • func findAll<T: Object>() -> [T] {
      return getRealm().objects(T.self).map { $0 }
    }