Notes
Notes - notes.io |
—————————————
//
// AddStudentVC.swift
// CoreDataCRUD
//
// Created by bmiit on 5/9/22.
//
import UIKit
class AddStudentVC: UIViewController {
@IBOutlet weak var txtname: UITextField!
@IBOutlet weak var txtstd: UITextField!
@IBOutlet weak var txtschool: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func onClickAdd(_ sender: Any) {
if let name=txtname.text, let std=txtstd.text, let school=txtschool.text
{
let newstudent = Student(context: DBManager.share.context)
newstudent.name=name
newstudent.std=std
newstudent.school=school
DBManager.share.saveContext()
}
}
}
==================================================================
Scenedelegate
—————————
//
// SceneDelegate.swift
// CoreDataCRUD
//
// Created by bmiit on 5/9/22.
//
import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
DBManager.share.saveContext()
}
}
=======================================================================
Viewcontroller
—————————
//
// ViewController.swift
// CoreDataCRUD
//
// Created by bmiit on 5/9/22.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var studentTable: UITableView!
var students=[Student]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
students=DBManager.share.fetchStudent()
studentTable.reloadData()
}
}
extension ViewController: UITableViewDelegate,UITableViewDataSource
{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return students.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let astudent=students[indexPath.row]
cell.textLabel?.text=astudent.name
cell.detailTextLabel?.text=astudent.school
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCell.EditingStyle.delete
{
do{
try DBManager.share.context.delete(students[indexPath.row])
}
catch{
print("error in deleting")
}
DBManager.share.saveContext()
students.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
}
}
}
==================================
Addstudentvc
//
// AddStudentVC.swift
// CoreDataCRUD
//
// Created by bmiit on 5/9/22.
//
import UIKit
class AddStudentVC: UIViewController {
@IBOutlet weak var txtname: UITextField!
@IBOutlet weak var txtstd: UITextField!
@IBOutlet weak var txtschool: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func onClickAdd(_ sender: Any) {
if let name=txtname.text, let std=txtstd.text, let school=txtschool.text
{
let newstudent = Student(context: DBManager.share.context)
newstudent.name=name
newstudent.std=std
newstudent.school=school
DBManager.share.saveContext()
}
}
}
============================================================================
Dbmanager
//
// DBManager.swift
// CoreDataCRUD
//
// Created by bmiit on 5/9/22.
//
import UIKit
import CoreData
class DBManager{
static let share=DBManager()
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "CoreDataCRUD")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error (error), (error.userInfo)")
}
})
return container
}()
// MARK: - Core Data Saving support
lazy var context = persistentContainer.viewContext
func saveContext () {
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error (nserror), (nserror.userInfo)")
}
}
}
func fetchStudent()->[Student]
{
var student=[Student]()
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: Student.description())
do{
student = try context.fetch(fetchRequest) as! [Student]
}
catch{
print("fetching error")
}
return student
}
}
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team