iOS开发——数据持久化Swift篇&(四)CoreData

时间:2024-12-04 16:37:19

CoreData

 import CoreData

 class ViewController: UIViewController {

     override func viewDidLoad() {
         super.viewDidLoad()
         // Do any additional setup after loading the view, typically from a nib.

         //获取管理的数据上下文对象
 //        let app = UIApplication.sharedApplication().delegate as AppDelegate

         //2015年5月2号修改
         let app = UIApplication.sharedApplication().delegate as! AppDelegate

         let context = app.managedObjectContext!

         var error:NSError?

         //创建User对象
 //        var oneUser = NSEntityDescription.insertNewObjectForEntityForName("User", inManagedObjectContext: context) as User

         //2015年5月2号修改
         var oneUser = NSEntityDescription.insertNewObjectForEntityForName("User", inManagedObjectContext: context) as! User

         //对象赋值
         oneUser.userID =
         oneUser.userEmail = "18500@126.com"
         oneUser.userPawd = "

         //保存
         if(!context.save(&error))
         {
             println("不能保存:\(error?.localizedDescription)");
         }

         //------- 查询

         //声明数据的请求
         var fetchRequest:NSFetchRequest = NSFetchRequest()
         fetchRequest.fetchLimit = // 限定查询结果的数量
         fetchRequest.fetchOffset = // 查询的偏移量

         //声明一个实体结构
         var entity:NSEntityDescription? = NSEntityDescription.entityForName("User", inManagedObjectContext: context)

         //设置数据请求的实体结构
         fetchRequest.entity = entity

         //设置查询条件
         let predicate = NSPredicate(format: "userID = '2'", "")
         fetchRequest.predicate = predicate

         //查询操作
         var fetchedObjects:[AnyObject]? = context.executeFetchRequest(fetchRequest, error: &error)

         //遍历查询的结果
 //        for info:User in fetchedObjects as [User] {

         //2015年5月2号修改
         for info:User in fetchedObjects as! [User] {
             println("userID = \(info.userID)")
             println("userEmail = \(info.userEmail)")
             println("userPawd = \(info.userPawd)")
             println("++++++++++++++++++++++++++++++++++++++")
         }

         //修改
         //遍历查询出来的所有对象
 //        for info:User in fetchedObjects as [User] {

         //2015年5月2号修改
         for info:User in fetchedObjects as! [User] {
             println("userID = \(info.userID)")
             println("userEmail = \(info.userEmail)")
             println("userPawd = \(info.userPawd)")
             println("++++++++++++++++++++++++++++++++++++++")

             //修改邮箱
             info.userEmail = "18500_junfei521@126.com"
             //重新保存
             if(!context.save(&error))
             {
                 println("不能保存:\(error?.localizedDescription)");
             }
         }

         //删除对象
 //        for info:User in fetchedObjects as [User]

         //2015年5月2号修改
         for info:User in fetchedObjects as! [User]
         {
             context.deleteObject(info)
         }
         //重新保存-更新到数据库
         if(!context.save(&error))
         {
             println("删除后保存:\(error?.localizedDescription)");
         }

         //获取路径
         let homeDirectory = NSHomeDirectory()
         println(homeDirectory)
     }

     override func didReceiveMemoryWarning() {
         super.didReceiveMemoryWarning()
         // Dispose of any resources that can be recreated.
     }