Getting a thread 1 exc bad instruction error when saving image litteral to core data swift

My swift code below is trying to save 2 image litterals to a core data attribut named pic. I am getting a error at newUser.pic = #imageLiteral(resourceName: “Jessica”).jpegData(compressionQuality: 0.9) as NSObject? STATING Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0). I dont know why this runtime error is occuring. As soon as the code builds it shunts down and shows me this error.

                 import UIKit
   import CoreData

    class ViewController: UIViewController {


   let appDelegate = UIApplication.shared.delegate as! AppDelegate //Singlton instance

     lazy var context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

      var piczy = [UIImage]()

     override func viewDidLoad() {
super.viewDidLoad()

let newUser = User(context: context)
let newUser2 = User(context: context)
newUser.pic = #imageLiteral(resourceName: "Jessica").jpegData(compressionQuality: 0.9) as NSObject?


newUser2.pic =  #imageLiteral(resourceName: "Jessica").jpegData(compressionQuality: 0.9) as NSObject?



save()

 }
   func save() {

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "User")

do {

 let jake =   try context.count(for: fetchRequest)


    print(jake)

} catch let error as NSError {
    print("Could not fetch \(error) ")
}}

@timswift
Without knowing your model setup, it is tough to say. When I see “EXC_BAD_INSTRUCTION”, it usually means that you the data is missing or mismatched.
My first guess is the image named “Jessica” is not in the Asset catalog. If an image is missing and you are trying to access it, it will throw this message.

I would suggest also changing your model User.pic from Transformable to Binary Data and drop the cast to NSObject. You may have to decode the image when you access it but you avoid casting Data to an obscure type.

You are also not saving the user objects when after you are adding the picture.

So to get this to work on your end, add an image named Jessica.png (or .jpg) to Assets.xcassets. Then update this ViewController to save the context.
Here is a working sample saving your core data objects.

  override func viewDidLoad() {
        super.viewDidLoad()

        let newUser = User(context: context)
        let newUser2 = User(context: context)
        newUser.pic = #imageLiteral(resourceName: "Jessica").jpegData(compressionQuality: 0.9)
        newUser2.pic =  #imageLiteral(resourceName: "Jessica").jpegData(compressionQuality: 0.9)
        save()
        fetchUserCount()
    }

    func save() {
        do {
            try context.save()
        } catch let error as NSError {
            print("Could not save \(error) ")
        }
    }

    func fetchUserCount() {
        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "User")
        do {
            let jake =   try context.count(for: fetchRequest)
            print(jake)
        } catch let error as NSError {
            print("Could not fetch \(error) ")
        }
    }

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