below is not working throwing an error , please help how to write a code for fetching data from Users entity.
attached screenenter image description here
below is not working throwing an error , please help how to write a code for fetching data from Users entity.
attached screenenter image description here
Grabbing appDelegate
object and context
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
Adding a record in Core Data
let user = NSEntityDescription.insertNewObject(forEntityName: "UsersClass", into: context) as! UsersClass
user.userName = "Rajan"
appDelegate.saveContext()
Fetching
let fetchRequest:NSFetchRequest<UsersClass> = UsersClass.fetchRequest()
And then execute this request
do {
let searchResults = try context.fetch(fetchRequest)
for trans in searchResults {
print(trans.username!)
}
}
catch {
//Handle error
}
UsersClass+CoreDataProperties.swift
import Foundation
import CoreData
extension UsersClass {
@nonobjc public class func fetchRequest() -> NSFetchRequest<UsersClass> {
return NSFetchRequest<UsersClass>(entityName: "UsersClass");
}
@NSManaged public var userName: String?
}
Here is the Sample which I made.