In my swift code below the goal is fetch all of the object names of the 3 images saved and print them in the debug section of Xcode. I have a fetch method but I don’t know how to get the specific object names printed in the debug section. There are 2 classes a base class and helper class. The helper class has the fetch method.
import UIKit; import CoreData
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .orange
let gwen = UIImage(named: "blank")
let katy = UIImage(named: "g.jpg")
let sarah = UIImage(named: "drg")
if let imageData = gwen.self?.pngData() {
CoredataHandler.shareInstance.saveImage(data: imageData)
}
if let imageData2 = katy.self?.pngData() {
CoredataHandler.shareInstance.saveImage(data: imageData2)
}
if let imageData3 = gwen.self?.pngData() {
CoredataHandler.shareInstance.saveImage(data: imageData3)
}
CoredataHandler.shareInstance.fetchImage()
}}
class CoredataHandler : NSManagedObject {
static let shareInstance = CoredataHandler()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
private class func getContext() -> NSManagedObjectContext {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
return appDelegate.persistentContainer.viewContext
}
func saveImage(data: Data) {
let imageInstance = Information(context: context)
imageInstance.pic = data
do {
try context.save()
} catch {
print(error.localizedDescription)
}
}
func fetchImage() -> [Information] {
var fetchingImage = [Information]()
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Information")
do {
fetchingImage = try context.fetch(fetchRequest) as! [Information]
} catch {
print("Error while fetching the image")
}
return fetchingImage
}
}