保存数据
新建Entity
代码实现
import UIKit
import CoreData
class ViewController: UIViewController {
override func viewDidLoad(){
super.viewDidLoad()
let context = (UIApplication.sharedApplication()
.delegate as! AppDelegate)
.managedObjectContext
let row:AnyObject = NSEntityDescription.insertNewObjectForEntityForName("Users", inManagedObjectContext: context)
row.setValue("jikexueyuan", forKey: "name")
row.setValue(1, forKey: "age")
do{
try context.save()
}catch{
print(error)
}
}
}
- 注意要导入CoreData
- 这样赋值只能是单个数据,如果要添加多条数据,需要循环
打印数据文件路径
AppDelegate
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("SingleViewCoreData.sqlite")
print(url)
......
//Result
file:///Users/jackliu/Library/Developer/CoreSimulator/Devices/A2C2C116-004D-4A10-95F0-654B4D89D1EB/data/Containers
/Data/Application/F3D3A90B-A29D-42C9-A699-3FC44EF0D2CA/Documents/
Application Sqlitebrowser
读取数据
配置环境
import UIKit
import CoreData
class UsersTableViewController: UITableViewController {
var dataArr:Array<AnyObject> = []
var context:NSManagedObjectContext!
override func viewDidLoad() {
super.viewDidLoad()
context = (UIApplication.sharedApplication()
.delegate as! AppDelegate)
.managedObjectContext
refreshData()
}
从sqlite数据库中读取
func refreshData(){
let f = NSFetchRequest(entityName: "Users")
do{
try dataArr = context.executeFetchRequest(f)
}catch{
print(error)
}
tableView.reloadData()
}
KV取值配置Cell
override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)as UITableViewCell
let label = cell.viewWithTag(2) as! UILabel
let name: AnyObject! = dataArr[indexPath.row].valueForKey("name")
let age: AnyObject! = dataArr[indexPath.row].valueForKey("age")
label.text = "name:\(name),age:\(age)"
return cell
}
viewWithTag( )
通过标签找到子视图
let label = cell.viewWithTag(2) as! UILabel
点击Cell跳转视图
override func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) {
let data = dataArr[indexPath.row] as! NSManagedObject
let vc = storyboard!.instantiateViewControllerWithIdentifier("UserContent")as! UserContentViewController
vc.data = data
presentViewController(vc, animated: true, completion: nil)
}
视图跳转 - StoryboardId
let vc = storyboard!.instantiateViewControllerWithIdentifier("UserContent")as! UserContentViewController
vc.data = data
presentViewController(vc, animated: true, completion: nil)
返回到tableView视图时
override func viewWillAppear(animated: Bool) {
refreshData()
}
func refreshData(){
let f = NSFetchRequest(entityName: "Users")
do{
try dataArr = context.executeFetchRequest(f)
}catch{
print(error)
}
tableView.reloadData()
}
删除数据
override func tableView(tableView: UITableView,canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView,commitEditingStyle editingStyle: UITableViewCellEditingStyle,forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
//从数据库删除
context.deleteObject(dataArr[indexPath.row] as! NSManagedObject)
//存档
do {
try context.save()
} catch _ {
}
//刷新
refreshData()
} else if editingStyle == .Insert {
}
}
接收到传递过来的CoreData数据
UserContentViewController
取值
var data:NSManagedObject!
tfName.text = data.valueForKey("name") as? String
let age: AnyObject! = data.valueForKey("age")
tfAge.text = "\(age)"
赋值存档
data.setValue(tfName.text, forKey: "name")
data.setValue(Int(tfAge.text!), forKey: "age")
do{
try data.managedObjectContext!.save()
}catch{
print(error)
}