0

I've been trying to do a simple CoreData task, saving data. I'm sure it works in Beta 6, but errors starting appearing after updating to Beta 7.

I think I have to add '?' or '!' based on the error hint, but just not smart enough to figure out where!

    @IBAction func saveItem(sender: AnyObject) {

    // Reference to App Delegate

    let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate

    // Reference our moc (managed object content)

    let contxt: NSManagedObjectContext = appDel.managedObjectContext!
    let ent = NSEntityDescription.entityForName("List", inManagedObjectContext: contxt)

    // Create instance of our data model and initialize

    var newItem = Model(entity: ent, insertIntoManagedObjectContext: contxt)

    // Map our attributes

    newItem.item = textFieldItem.text
    newItem.quanitity = textFieldQuantity.text
    newItem.info = textFieldInfo.text

    // Save context

    contxt.save(nil) 
}

The error says

Value of optional type 'NSEntityDescription?' not unwrapped; did you mean to use '!' or '?'

At the line

var newItem = Model(entity: ent, insertIntoManagedObjectContext: contxt)

Everytime I seem to have clear the error and compiles ok, clicking the 'Save' shows in the debug area

fatal error: unexpectedly found nil while unwrapping an Optional value
Imanou Petit
  • 89,880
  • 29
  • 256
  • 218

2 Answers2

0

The error is fairly trivial, there's not much to analyze here. Try changing this:

let ent = NSEntityDescription.entityForName("List", inManagedObjectContext: context)

to this

let ent = NSEntityDescription.entityForName("List", inManagedObjectContext: context)!

As always, novices tend to overlook tell-tale signs. The error clearly states that the optional is of type NSEntityDescription. And given that there is only object of this type being instantiated in the given code, it doesn't take a genius to guess where the error lies.

Value of optional type 'NSEntityDescription?' not unwrapped; did you mean to use '!' or '?'

Also, method used here to instantiate the NSEntityDescription object is declared as follows:

class func entityForName(entityName: String, inManagedObjectContext context: NSManagedObjectContext) -> NSEntityDescription? 

... the ? character clearly telling us that this method returns an optional.

Vatsal Manot
  • 17,695
  • 9
  • 44
  • 80
0

I presume that the Model initializer signature is:

init(entity: NSEntityDescription, insertIntoManagedObjectContext: NSManagedObjectContext)

the compilation error happens because NSEntityDescription.entityForName returns an optional, so you have to unwrap it.

As for the runtime error, my guess is that contxt is nil, and you are passing a forced unwrapped here:

let ent = NSEntityDescription.entityForName("List", inManagedObjectContext: contxt)

To made code safer and more clear, I'd explicitly use optionals:

let contxt: NSManagedObjectContext? = appDel.managedObjectContext
if let contxt = contxt {
    let ent: NSEntityDescription? = NSEntityDescription.entityForName("List", inManagedObjectContext: contxt)

    // Create instance of our data model and initialize

    if let ent = ent {
        var newItem = Model(entity: ent, insertIntoManagedObjectContext: contxt)
    }
}

and use debugger & breakpoints to check if any of the mentioned variable is nil.

Antonio
  • 71,651
  • 11
  • 148
  • 165