etayluz etayluz - 2 months ago 21
Swift Question

Realm iOS: Cannot Convert value of type 'Dogs.Type' to expected argument type 'T.Type'

Here is my DBManager.swift

import RealmSwift

class DBManager {

class func getAllDogs() -> [Dog] {

let realm = try! Realm()
// COMPILER ERROR: Cannot Convert value of type 'Dogs.Type' to expected argument type 'T.Type'
return realm.objects(Dog.self)

}

}


This is my Dog.swift:

import Foundation
import RealmSwift

class Dog: Object {
dynamic var firstName = ""
dynamic var lastName = ""
dynamic var dateOfBirth = ""
dynamic var lastFourSsn = ""
dynamic var currentLocation = ""
dynamic var dog: NSData!
}


I am getting this compiler error:


Cannot Convert value of type 'Dogs.Type' to expected
argument type 'T.Type'


Seems like I'm missing something really basic here - because this is mostly code directly from the RealmSwift documentation. What am I doing wrong?

Answer

realm.objects(Dog.self) returns Results<Dog>, not Array<Dog> (a.k.a. [Dog]). The misleading error message is probably just the compiler failing to do a good job of reporting where exactly the type error occurred.

Comments