Deniss Deniss - 2 years ago 2427
Swift Question

Swift 3 Beta, NSFetchrequest

need some help and I can't figure it out,

before line was working:

let request = NSFetchRequest(entityName: String)

but in swift 3 beta it give error (generic parameter "ResultType" could not be inferred), because NSFetchRequest is now a parameterized type based. So in their documents they wrote this:

let request: NSFetchRequest<Animal> = Animal.fetchRequest

so if my String is for example "Level" how should I right request now ?

because this not working:

let request: NSFetchRequest<Level> = Level.fetchRequest

thanks for help!

Answer Source
let request: NSFetchRequest<NSFetchRequestResult> = Level.fetchRequest()


let request: NSFetchRequest<Level> = Level.fetchRequest()

depending which version you want.

The first version is defined for NSManagedObject, the second version is generated automatically for every object using an extension, e.g:

extension Level {
    @nonobjc class func fetchRequest() -> NSFetchRequest<Level> {
        return NSFetchRequest<Level>(entityName: "Level");

    @NSManaged var timeStamp: NSDate?

The whole point is to remove the usage of String constants.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download