日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

IOS之Swift的CoreData入门使用案例

發(fā)布時間:2023/12/18 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOS之Swift的CoreData入门使用案例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

IOS之Swift的CoreData入門使用案例

CoreData和SQLite3類似,用來把數(shù)據(jù)存在磁盤上的。可以隨時讀寫。


創(chuàng)建項目鉤上

當APP退出的時候,數(shù)據(jù)消失。
使用CoreData,退出App,重啟或 退出App,點擊進去App,數(shù)據(jù)仍然顯示
核心代碼在這;

// // DJTableViewController.swift // HitList // // Created by 魯軍 on 2021/4/7. //import UIKit import CoreDataclass DJTableViewController: UITableViewController {var people: [NSManagedObject] = []var names: [String] = []@IBAction func test(_ sender: Any) {debugPrint("asd1")/*let alert = UIAlertController(title: "New Name",message: "Add a new name",preferredStyle: .alert)let saveAction = UIAlertAction(title: "Save",style: .default) {[unowned self] action inguard let textField = alert.textFields?.first,let nameToSave = textField.text else {return}self.names.append(nameToSave)self.tableView.reloadData()}let cancelAction = UIAlertAction(title: "Cancel",style: .default)alert.addTextField()alert.addAction(saveAction)alert.addAction(cancelAction)present(alert, animated: true)*/let alert = UIAlertController(title: "New Name",message: "Add a new name",preferredStyle: .alert)let saveAction = UIAlertAction(title: "Save",style: .default) {[unowned self] action inguard let textField = alert.textFields?.first,let nameToSave = textField.text else {return}//self.names.append(nameToSave)save(name: nameToSave)self.tableView.reloadData()}let cancelAction = UIAlertAction(title: "Cancel",style: .default)alert.addTextField()alert.addAction(saveAction)alert.addAction(cancelAction)present(alert, animated: true)}func save(name: String) {guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {return}// 1let managedContext = appDelegate.persistentContainer.viewContext// 2let entity =NSEntityDescription.entity(forEntityName: "Person",in: managedContext)!let person = NSManagedObject(entity: entity,insertInto: managedContext)// 3person.setValue(name, forKeyPath: "name")// 4do {try managedContext.save()people.append(person)} catch let error as NSError {print("Could not save. \(error), \(error.userInfo)")}}override func viewDidLoad() {super.viewDidLoad()self.title = "The List"}override func viewWillAppear(_ animated: Bool) {super.viewWillAppear(animated)//1guard let appDelegate =UIApplication.shared.delegate as? AppDelegate else {return}let managedContext =appDelegate.persistentContainer.viewContext//2let fetchRequest =NSFetchRequest<NSManagedObject>(entityName: "Person")//3do {people = try managedContext.fetch(fetchRequest)} catch let error as NSError {print("Could not fetch. \(error), \(error.userInfo)")}}override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {let person = people[indexPath.row]let cell = tableView.dequeueReusableCell(withIdentifier: "Cell",for: indexPath) // cell.textLabel?.text = names[indexPath.row]cell.textLabel?.text = person.value(forKeyPath: "name") as? Stringreturn cell}// MARK: - Table view data sourceoverride func numberOfSections(in tableView: UITableView) -> Int {// #warning Incomplete implementation, return the number of sectionsreturn 1}override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {// #warning Incomplete implementation, return the number of rows // return self.names.countreturn self.people.count}}

總結(jié)

以上是生活随笔為你收集整理的IOS之Swift的CoreData入门使用案例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。