Generic Parameter Swift 3 Error

override func viewWillAppear(_ animated: Bool) {
        let appDelegate = UIApplication.shared().delegate as! AppDelegate
     
        let managedContext = appDelegate.managedObjectContext
     
        let fetchRequest = NSFetchRequest(entityName: "ListEntity")         Generic parameter 'Result Type' could not be inferred
     
        do {
            let results = try managedContext.fetch(fetchRequest)
            listItems = results as! [NSManagedObject]
         
        }
        catch {
            print("Error")
         
        }
     
    }


Error: Generic parameter 'Result Type' could not be inferred


This is my code and it change from swift 2.2 to 3 and now it gives me that error on the right. If anyone could help that would be greatly appreciated.

Thanks,

TechiBot

Accepted Reply

I resolved by doing that :

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "ListEntity")

Replies

I resolved by doing that :

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "ListEntity")

Thank you so much for the help. I have been very frustrated. Thanks, TechiBot

The solution above clears the error, however adding <NSFetchRequestResult> provides no benefits.


The correct solution is detailed here: https://developer.apple.com/library/prerelease/content/releasenotes/General/WhatNewCoreData2016/ReleaseNotes.html

This helps the compiler to expect [ListEntity] from our fetch request

let fetchRequest: NSFetchRequest<ListEntity> = ListEntity.fetchRequest


allowing this:

let searchResults = try context.fetch(request) //as? [ListEntity]


However, I can't get it to work as it autocompletes as ListEntity.fetchRequest()

Please feedback if you find how to get this to work.

This save me app! thank u so much!!!😁

When I try the solutions proposed by Jimmy, I get another error:


" Use of undeclared type 'ListEntity' "


Of course, I am not writing "ListEntity" but the name of my own entitiy. Have anybody experienced this?

Please post a copy of the line that’s triggering the problem.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"