0

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?

alex
  • 4,804
  • 14
  • 51
  • 86
  • 2
    You're using Swift and I betting $10 you didn't namespace the class name when you created the entity. On the right pane where the class name is, you have to add the name space via className: MyProject.InvoiceList – TheCodingArt Sep 02 '15 at 19:10

1 Answers1

0

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

TheCodingArt
  • 3,436
  • 4
  • 30
  • 53
  • hmm, i am trying this but xcode 7 has a slightly different UI and xcode doesn't let me MyProject.InvoicesList. The period is deleted as soon as i hit return/tab so it reads MyProjectInvoicesList??? – alex Sep 02 '15 at 19:57
  • ARRGGGh, found it ..................typo issue i had `entityForName("InvoiceList",` and not `entityForName("InvoicesList",` – alex Sep 02 '15 at 20:02
  • 1
    Glad to see you found a solution to the issue. In Xcode 7 the module should be set to your project by default (it will be under the module field). In Xcode 6 you had to specify the module as shown above (which is the same thing as the namespace). – TheCodingArt Sep 02 '15 at 20:04