zoukankan      html  css  js  c++  java
  • 24_Core Data Demo

    今天开始学习Core Data,类似于数据库,可以永久保存数据。不过当把App从iPhone删掉之后就没有了。可以用来保存App的运行数据。

    参考链接:iOS Swift教程 Core Data 概述

    Part1:

    //
    //  ViewController.swift
    //  Core Data Demo
    //
    //  Created by zcdll on 16/1/22.
    //  Copyright © 2016年 ZC. All rights reserved.
    //
    
    import UIKit
    import CoreData
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            
            let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            
            let context: NSManagedObjectContext = appDel.managedObjectContext
            
            //第一次添加时使用,不注释会每次添加一次
            /*
            let newUser = NSEntityDescription.insertNewObjectForEntityForName("Users", inManagedObjectContext: context)
            
            newUser.setValue("zcdll", forKey: "username")
            
            newUser.setValue("asdf", forKey: "password")
            
            do {
                
                try context.save()
                
            } catch {
                
                print("Save problem")
                
            }
            */
            
            let request = NSFetchRequest(entityName: "Users")
            
            do {
                
                let results = try context.executeFetchRequest(request)
                
                if results.count > 0 {
                    
                    for result in results as! [NSManagedObject] {
                        
                        print(result.valueForKey("username")!)
                        
                        print(result.valueForKey("password")!)
                        
                    }
                    
                }
                
            } catch {
                
                print("Fetch failed")
                
            }
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }
    

    工程:24_Core_Data_Demo

    Part2:

    增加了更新数据,删除数据,查询数据等功能,感觉这样写很不灵活。

    //
    //  ViewController.swift
    //  Core Data Demo
    //
    //  Created by zcdll on 16/1/22.
    //  Copyright © 2016年 ZC. All rights reserved.
    //
    
    import UIKit
    import CoreData
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            
            let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            
            let context: NSManagedObjectContext = appDel.managedObjectContext
            
            //第一次添加时使用,不注释会每次添加一次
            /*
            let newUser = NSEntityDescription.insertNewObjectForEntityForName("Users", inManagedObjectContext: context)
            
            newUser.setValue("zcdll12", forKey: "username")
            
            newUser.setValue("asdf12", forKey: "password")
            
            do {
                
                try context.save()
                
            } catch {
                
                print("Save problem")
                
            }
            */
            
            let request = NSFetchRequest(entityName: "Users")
            
            //request.predicate = NSPredicate(format: "username = %@", "zcdll3")
            
            request.returnsObjectsAsFaults = false
            
            do {
                
                let results = try context.executeFetchRequest(request)
                
                if results.count > 0 {
                    
                    for result in results as! [NSManagedObject] {
                        
                        /*
                        
                        context.deleteObject(result)
                        //result.setValue("zcdll3",forKey: "username")
                        
                        do {
                            
                            try context.save()
                            
                        } catch {}
                        
                        */
                        
                        if let username = result.valueForKey("username") as? String {
                        
                            print(username)
                            
                        }
                        
                        //print(result.valueForKey("password")!)
                        
                    }
                    
                }
                
            } catch {
                
                print("Fetch failed")
                
            }
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }
    

    工程:24_Core_Data_Demo

  • 相关阅读:
    洛谷 P2058 海港(模拟)
    LA 3708 墓地雕塑(模拟)
    Uva 11300 Spreading the Wealth(贪心)
    UVA 11729 Commando War (贪心)
    【洛谷习题】Likecloud-吃、吃、吃
    【洛谷习题】多米诺骨牌
    【洛谷习题】相似基因
    【NOI1995】石子合并
    【洛谷习题】尼克的任务
    【NOIP2004】合唱队形
  • 原文地址:https://www.cnblogs.com/zcdll/p/5152681.html
Copyright © 2011-2022 走看看