Core Data Swift 4 (Insert, delete and display)

Опубликовано: 26 Май 2026
на канале: Divyesh Gondaliya
257
11

Today we go over how to save data to the device with Core Data using Swift 4.(code is in the description)

Core Data is an Apple framework that allows you to store user data to the device and ensure that it can load saved data that persists even after app termination. With Core Data, you can structure your data models with both code and a visual representation, save complex objects, and fetch those complex objects. Learning how to persist data to the device is very import for most applications. Know how to save data can be a valuable tool to add to your arsenal.

Note: This tutorial has been updated (03-Aug-2018)to use Xcode 9.4.1 and Swift 4.

Collectionview inside ScrollView :-    • Collectionview inside Scrollview  
Dynamic height TableView in a Scroll View :-    • Dynamic height TableView in a Scroll View  

My Social Media link is below
-------------
instagram :-   / gdivyesh40  
facebook :- https://www.facebook.com/profile.php?...

----------for save-------------
var person_DB = [NSManagedObject]()

func save_person(name:String,address:String) {
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext:NSManagedObjectContext = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Person", in:managedContext)

let nottmp = NSManagedObject(entity: entity!, insertInto: managedContext)

nottmp.setValue(name, forKey: "name")
nottmp.setValue(address, forKey: "address")
do {
person_DB.append(nottmp)
print ("Person Save Done")

}
}

-------------- for delete---------------
func deleteAllPerson() {
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext:NSManagedObjectContext = appDelegate.persistentContainer.viewContext

let deleteFetch = NSFetchRequest'-lt'NSFetchRequestResult'-gt'(entityName: "Person")
let deleteRequest = NSBatchDeleteRequest(fetchRequest: deleteFetch)
do {
try managedContext.execute(deleteRequest)
try managedContext.save()
print ("RemoveAll deleteAllPerson Done")
} catch {
print ("There was an error Person RemoveAll")
}
}

--For fatch data--
func FatchingData_all()-"-gt"[PersonDataProvider]{

var pass_data = [PersonDataProvider]()

guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return pass_data
}
let managedContext:NSManagedObjectContext = appDelegate.persistentContainer.viewContext
let fetchRequest:NSFetchRequest"-lt"NSFetchRequestResult"-gt" = NSFetchRequest(entityName: "Person")
let sort = NSSortDescriptor(key: #keyPath(Person.name), ascending: true)
fetchRequest.sortDescriptors = [sort]
do {
let results = try managedContext.fetch(fetchRequest) as! [NSManagedObject]
if (results.count "-gt" 0){
for item in results
{
pass_data.append(PersonDataProvider(dic: item))
}
}
}catch let error {
print("Error....: \(error)")
}
return pass_data
}

class PersonDataProvider:NSObject
{
var name:String
var add:String

init(dic:NSManagedObject) {
self.name = dic.value(forKey: "name") as? String ?? ""
self.add = dic.value(forKey: "address") as? String ?? ""
}
}