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 {
}
}
}