coredata数据的保存与查询
来源:互联网 发布:淘宝情趣内衣网店 编辑:程序博客网 时间:2024/05/17 02:08
以下是我对coreData在swift中的应用的一个简单的理解,这里我会介绍数据的以及数据的查询,已经每个语句所代表的意义
首先我们需要导入coreData的头文件
import CoreData
然后声明一个数组用来存放coreData的实例
var people=[NSManagedObject]()
以下我是通过uitableview来进行数据的展示,因此代码如下
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return people.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell=tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell
var person=people[indexPath.row]//获得存储在数组中的实例
cell.textLabel!.text=person.valueForKey("name") as! String?//获取该实例所存储的key值为name的值
return cell
}
//将数据保存到数据库,KVC模式
func saveName(name:String){
let appDelegate=UIApplication.sharedApplication().delegate as! AppDelegate
let managerContext=appDelegate.managedObjectContext! //获取上下文实例
let entity=NSEntityDescription.entityForName("Person", inManagedObjectContext: managerContext) //获取上下文中名字为Person的实体
let person=NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managerContext)//将entity插入上下文,并拿到这个实例,!表示强制认为变量entity不是nil
person.setValue(name, forKey: "name")//赋值
var error:NSError?
//如果发现保存错误,那么就不再执行,并输出错误信息
if !managerContext.save(&error){
println("Could not save \(error),\(error?.userInfo)")
}
people.append(person)
}
//查询数据库
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let appDelegate=UIApplication.sharedApplication().delegate as! AppDelegate
let contextManager=appDelegate.managedObjectContext
let fetchRequest=NSFetchRequest(entityName: "Person")//查询名字为Person的实例
var error:NSError?
let fetchResults=contextManager?.executeFetchRequest(fetchRequest, error: &error) as! [NSManagedObject]?
//如果获得实例的值不为空,那么就赋值给people数组(得到的结果也是一个数组)
if let results=fetchResults{
people=results
}
}
例子的话我放在我的github上,欢迎下载
首先我们需要导入coreData的头文件
import CoreData
然后声明一个数组用来存放coreData的实例
var people=[NSManagedObject]()
以下我是通过uitableview来进行数据的展示,因此代码如下
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return people.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell=tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell
var person=people[indexPath.row]//获得存储在数组中的实例
cell.textLabel!.text=person.valueForKey("name") as! String?//获取该实例所存储的key值为name的值
return cell
}
//将数据保存到数据库,KVC模式
func saveName(name:String){
let appDelegate=UIApplication.sharedApplication().delegate as! AppDelegate
let managerContext=appDelegate.managedObjectContext! //获取上下文实例
let entity=NSEntityDescription.entityForName("Person", inManagedObjectContext: managerContext) //获取上下文中名字为Person的实体
let person=NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managerContext)//将entity插入上下文,并拿到这个实例,!表示强制认为变量entity不是nil
person.setValue(name, forKey: "name")//赋值
var error:NSError?
//如果发现保存错误,那么就不再执行,并输出错误信息
if !managerContext.save(&error){
println("Could not save \(error),\(error?.userInfo)")
}
people.append(person)
}
//查询数据库
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let appDelegate=UIApplication.sharedApplication().delegate as! AppDelegate
let contextManager=appDelegate.managedObjectContext
let fetchRequest=NSFetchRequest(entityName: "Person")//查询名字为Person的实例
var error:NSError?
let fetchResults=contextManager?.executeFetchRequest(fetchRequest, error: &error) as! [NSManagedObject]?
//如果获得实例的值不为空,那么就赋值给people数组(得到的结果也是一个数组)
if let results=fetchResults{
people=results
}
}
例子的话我放在我的github上,欢迎下载
https://github.com/dyljqq/CoreDataCourse.git
0 0
- coredata数据的保存与查询
- coredata 分页查询数据
- iOS CoreData 查询数据
- CoreData-实现数据永久性保存时产生的问题
- CoreData-实现数据永久性保存时遇到的问题
- Coredata第三课 数据查询
- Coredata第三课 数据查询
- CoreData的数据迁移
- CoreData的数据迁移
- CoreData添加数据和查询数据
- 保存数据同时查询保存数据记录的ID
- CoreData能根据主键查询数据吗
- CoreData能根据主键查询数据吗
- CoreData和ios开发中的数据库,直接查询模拟器内的sqlite数据
- SQLite 时间的保存与查询
- CoreData 简单的查询、修改 无监听
- coredata 查询操作的核心代码
- 过滤器NSPredicate的Coredata 子查询 SUBQUERY
- ThinkPHP--添加数据记录add (实现数据添加)
- test9.14
- WireShark使用教程
- 观《当幸福来敲门》有感
- 第62讲:Scala中上下文界定内幕中的隐式参数与隐式参数的实战详解及其在Spark中的应用源码解析学习笔记
- coredata数据的保存与查询
- ThinkPHP--收集表单数据并实现收集添加
- HashMapS深度解析(一)
- 观《赢在中国》有感
- HashMap深度解析(二)
- 常用网络命令的使用简介
- ASP 页的执行造成响应缓冲区超过其配置限制
- TP-LINK路由器WR703N刷openwrt固件挂载USB摄像头(下)
- test9.15