Runtime error when saving user generated image to core data (Swift5)

I am trying to save a user generated image to core data. If I program the image the code works fine. But when the user selects the image I am getting the error Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value at jx = drawPlace.image! . I just want to fix that and the code will work. It is not having any problems saving into core data just the wrapping.

                                  var jx = UIImage()
                   var drawPlace = UIImageView()
           func save() {
            guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }

let managedContext = appDelegate.persistentContainer.viewContext

let entity = NSEntityDescription.entity(forEntityName: "Item", in: managedContext)!

let item = NSManagedObject(entity: entity, insertInto: managedContext)


        jx = drawPlace.image!
if let data = jx.pngData() {
    item.setValue(data, forKey: "image")
}

@timswift Do you still have issues with this?

This topic was automatically closed after 166 days. New replies are no longer allowed.