Deleting a binary data image by a instance

In my Swift code below the goal is to delete a binary data by the corresponding var Int. The var Int is called counterImage. My code right now is causing a runtime error of counterImage

‘Cannot convert value of type ‘Int’ to expected argument type ‘TheBook’’

What can I do to fix this? All the code is right here. You would have to add the images but after that you can just copy and paste the code.

import UIKit;import CoreData

class ViewController: UIViewController {   
    var counterImage = 1    

    override func viewDidLoad() {
        super.viewDidLoad()

        let gwen = UIImage(named: "h")
        if let imageData = gwen.self?.pngData() {
            helpImage.shareInstance.saveImage(data: imageData)
        }
        let gwen2 = UIImage(named: "hh")
        if let imageData = gwen2.self?.pngData() {
            helpImage.shareInstance.saveImage(data: imageData)
        }
        helpImage.shareInstance.deleteObject(user: counterImage)  
    }
}

class helpImage: UIViewController{
    private class func getContext() -> NSManagedObjectContext {
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        return appDelegate.persistentContainer.viewContext
    }

    static let shareInstance = helpImage()
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

    func saveImage(data: Data) {
        let imageInstance = TheBook(context: context)
        imageInstance.pic = data  
        do {
            try context.save()
        } catch {
            print(error.localizedDescription)
        }
    }

    func deleteObject(user: TheBook) {
        let context = helpImage.getContext()
        let delete = NSBatchDeleteRequest(fetchRequest: TheBook.fetchRequest())
        do {
            try context.execute(delete)
        } catch {
        }
    }    
}

You are getting the error because you are supplying an integer to a function that takes an argument of type TheBook.

The deleteObject function takes an object of type TheBook as an argument.

func deleteObject(user: TheBook)

When you call deleteObject in viewDidLoad, you pass counterImage as the argument to the function. You declared counterImage to be of type Int by assigning it the value 1.

var counterImage = 1

To fix the error, pass an instance of type TheBook to deleteObject. You don’t create an instance of TheBook in the code you showed so you will have to create an instance somewhere in your app and pass it as an argument when calling deleteObject.

can you write exactly what i need for this work? I have been really struggling on this. Thanks for your support.

I told you what you need to do to fix the error. Create an instance of TheBook and pass it as the user argument when you call deleteObject. No one here is going to write your code for you.

What part of my suggestion are you struggling with? The first two lines of your saveImage function create an instance of TheBook.

I can’t provide more specific advice because I don’t understand what your ViewController class does. It creates two images then deletes one in viewDidLoad. Why does the code do that? Wouldn’t it be easier to just create one image.

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