vendredi 27 mars 2015

iOS 8 Core Data stack - fatal error: found nil while unwrapping an Optional value


Vote count:

0




I'm relatively new to iOS Development and decided to implement my own Core Data stack, replacing Apple's default stack.


I've had to make changes in my code (obviously) and have been able to figure it out, however in this instance I cannot. Here is my code:



import UIKit
import CoreData

class AddTableViewController: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var nameTextField:UITextField!
@IBOutlet weak var locationTextField:UITextField!
@IBOutlet weak var imageView:UIImageView!
@IBOutlet weak var notesView:UITextView!

var coreDataStack = (UIApplication.sharedApplication().delegate as AppDelegate).coreDataStack

var backpackerSpot:BackpackerSpot!
var managedContext: NSManagedObjectContext!

override func viewDidLoad() {
super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// TODO Give user the choice of the Photo Library or the Camera
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == 0 {
if UIImagePickerController.isSourceTypeAvailable(.Camera) {
let imagePicker = UIImagePickerController()
imagePicker.allowsEditing = false
imagePicker.delegate = self
imagePicker.sourceType = .Camera

self.presentViewController(imagePicker, animated: true, completion: nil)
}
}

tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

// FIXME image is being displayed in landscape if it is taken in portrait mode by default
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
imageView.image = image
imageView.contentMode = UIViewContentMode.ScaleAspectFill
imageView.clipsToBounds = true

dismissViewControllerAnimated(true, completion: nil)
}

@IBAction func save() {

//validation
var errorField = ""

// TODO have placeholder text in the NOTES field match up with the placholder text in the NAME and LOCATION fields.
if nameTextField.text == "" {
errorField = "name"
} else if locationTextField.text == "" {
errorField = "location"
} else if notesView.text == "" {
errorField = "notes"
}

if errorField != "" {

let alertController = UIAlertController(title: "Error", message: "You must fill in \(errorField).", preferredStyle: .Alert)
let doneAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(doneAction)

self.presentViewController(alertController, animated: true, completion: nil)

return
}

// If all fields are correctly filled in, extract the field value
// Create Restaurant Object and save to data store
// if let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).coreDataStack.context {

let entityBackpackerSpot = NSEntityDescription.entityForName("BackpackerSpot", inManagedObjectContext: coreDataStack.context)

backpackerSpot?.spotName = nameTextField.text
backpackerSpot?.spotLocation = locationTextField.text
backpackerSpot?.spotImage = UIImagePNGRepresentation(imageView.image)
backpackerSpot?.spotNote = notesView.text

var error: NSError?
if !managedContext.save(&error) {
println("insert error: \(error!.localizedDescription)")
return
}
// Execute the unwind segue and go back to the home screen
performSegueWithIdentifier("unwindToHomeScreen", sender: self)
}

}


The app starts up fine, but when I click on the UIButton attached to the save function, it crashes and I get the following error:



fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)


The line the debugger highlights is:



if !managedContext.save(&error) {


Admittedly, I have to spend some more time working with Optionals in Swift because they seem to often be a source of trouble for me, though usually I am able to figure it out. If anyone could point me in the right direction that would be great. Thank you.



asked 29 secs ago







iOS 8 Core Data stack - fatal error: found nil while unwrapping an Optional value

Aucun commentaire:

Enregistrer un commentaire