image.png
//项目创建成功后,在项目文件夹中将自动生成一个拥 有.xcdatamodelId扩展名的文件,该文件可以使用图形化的方式编辑数据 模型,在该文件中包含了Entities、Properties、Attributes、Relationships 4 个概念,和关系型数据库有很大的相似性
image.png
//首先点击底部的【Add Entity】按钮,添加一个新的实体,接着将 实体的名称修改为User。然后在【Attributes】设置区,点击 加号图标 为User实体添加一个新的属性userName,并设置Type类型为String。最 后使用相同的方式,为User实体添加第二个属性password。 //完成Entity模型的创建之后,接着来创建一个托管对象类 NSManagedObject。在项目名称文件夹上点击鼠标右键,然后选择菜单 中的【New File……】命令。在弹出的文件模板选择窗口中,依次选择 【Core Data>NSManagedObject subclass】选项,创建一个 NSManagedObject的子类
image.png
//然后点击【Next】按钮,并在接下来的选择待管理的实体页面中选 中【User】实体,从而创建一个名为User的NSManagedObject子类和名 为User CoreDataProperties.siwft的扩展文件,并在User.swift代码中添加 一行代码@objc(User),因为在Swift类型文件中,需要在暴露给OC使用 的类、属性和方法声明的前面,加上@objc修饰符
image.png
代码语言:javascript复制 1 import UIKit
2 import CoreData
3
4 class ViewController:UIViewController {
5 override func viewDidLoad() {
6 super.viewDidLoad()
7 // Do any additional setup after loading the view,
typically from a nib.
8
9 let appDelegate:AppDelegate =
UIApplication.shared().delegate as!AppDelegate
10 let managedObjectContext =
appDelegate.managedObjectContext
11
12 let newUser =
NSEntityDescription.insertNewObject(forEntityName:“User”,
into: managedObjectContext) as!User
13
14 newUser.userName = “John”
15 newUser.password = “123456”
16
17 do{
18 try managedObjectContext.save()
19 print(“成功保存数据。”)
20 }catch
21 {
22 print(“保存数据失败。”)
23 }
24 }
25 }
//2
1 import UIKit
2 import CoreData
3
4 class ViewController:UIViewController {
5
6 override func viewDidLoad() {
7 super.viewDidLoad()
8 // Do any additional setup after loading the view,
typically from a nib.
9
10 let appDelegate:AppDelegate =
UIApplication.shared.delegate as!AppDelegate
11 let managedObjectContext =
appDelegate.managedObjectContext
12
13 let entity:NSEntityDescription?=
NSEntityDescription.entity(forEntityName:“User”,
14 in:managedObjectContext)
15 let request = NSFetchRequest<User>
(entityName:“User”)
16 request.fetchOffset = 0
17 request.fetchLimit = 10
18 request.entity = entity
19
20 let predicate = NSPredicate(format:“userName=
‘John’ “, ””)
21 request.predicate = predicate
22
23 do{
24 let results:[AnyObject]?= try
managedObjectContext.fetch(request)
25
26 for user:User in results as![User]{
27 print(“userName=(user.userName!)”)
28 print(“password=(user.password!)”)
29 }
30 }
31 catch{
32 print(“获取数据失败。”)
33 }
34 }
35 }
代码语言:javascript复制//修改
1 import UIKit
2 import CoreData
3
4 class ViewController:UIViewController {
5 override func viewDidLoad() {
6 super.viewDidLoad()
7 // Do any additional setup after loading the view,
typically from a nib.
8 let appDelegate:AppDelegate =
UIApplication.shared.delegate as!AppDelegate
9 let managedObjectContext =
appDelegate.managedObjectContext
10
11 let entity:NSEntityDescription?=
NSEntityDescription.entity(forEntityName:“User”,
12 in:managedObjectContext)
13 let request = NSFetchRequest<User>
(entityName:“User”)
14 request.fetchOffset = 0
15 request.fetchLimit = 10
16 request.entity = entity
17
18 let predicate = NSPredicate(format:“userName=
‘Jerry’ “, ””)
19 request.predicate = predicate
20
21 do{
22 let results:[AnyObject]?= try
managedObjectContext.fetch(request)
23 for user:User in results as![User]{
24 user.userName = “Peter”
25 }
26 try managedObjectContext.save()
27
28 request.predicate = nil
29 let newResults:[AnyObject]?= try
managedObjectContext.fetch(request)
30
31 for user:User in newResults as![User]{
32 print(“userName:(user.userName)”)
33 print(“password:(user.password)”)
34 }
35 }
36 catch{
37 print(“获取数据失败。”)
38 }
39 }
40 }
代码语言:javascript复制//删除
23 for user:User in results as![User]{
24 managedObjectContext.delete(user)
25 }