Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSFetchRequest as entityName gives error "use of undeclared type"

I'm using Core data with Swift 2.1 and Xcode 7.2 This code gives me an error that can't find the entity name.This as [Company] doesn't work.I have a entity with this name.

    let fetchRequest = NSFetchRequest(entityName: "Company")

    do {
        var result = try self.moc.executeFetchRequest(fetchRequest) as! [Company]
        if (result.count > 0){

        companyName?.stringValue = result[0].valueForKeyPath("name") as! String

        // success ...
        }
        else{

        }
    } catch let error as NSError {
        // failure
        print("Fetch failed: \(error.localizedDescription)")
    }
like image 275
user2414590 Avatar asked Jan 11 '16 13:01

user2414590


2 Answers

Add to file head:

import CoreData
like image 171
Guryanov Andrey Avatar answered Nov 06 '22 17:11

Guryanov Andrey


You said you have an entity named "Company" in your model. Okay, but do you have an actual NSManagedObject class named Company (ie, class Company: NSManagedObject { ... }) that you created from within the managed object model editor in Xcode? This isn't automatic. Without an actual class declared somewhere, it will be an "undefined type" since the entity name is just a string to look things up in your model otherwise.

The fact the rest of your code is accessing properties by valueForKeyPath() strongly suggests there's no actual class Company: NSManagedObject anywhere.

like image 1
Joshua Nozzi Avatar answered Nov 06 '22 17:11

Joshua Nozzi