NSEntityDescription.entityForName returns nil how can i fix this?

369 views Asked by At

So i am getting the error:

fatal error: unexpectedly found nil while unwrapping an Optional value

After some trail and error i think it has to do with let ent see

func newItem(){

    let context = self.context
    let ent = NSEntityDescription.entityForName("InvoiceList", inManagedObjectContext: context)
     print("New data \(ent) ")//outputs: New data nil 

    let nItem = InvoicesList(entity: ent!, insertIntoManagedObjectContext: context)

ent seems to be nil, but i can't use ent? as in let nItem = InvoicesList(entity: ent?, insertIntoManagedObjectContext: context)

So how can i fix this? The core data entity InvoiceList has attributes but no values at start is this wrong?

1

There are 1 answers

3
TheCodingArt On

Ensure your Entity is setup using a namespace as noted in the image below. On the right pane where the class name is, you have to add the name space via className: MyProject.InvoiceList

image