002 Core Data 시리즈

1362 단어
Core Data가 어떻게 작동하는지 간단한 데모 프레젠테이션
Core Data 스토리지 데이터
func saveName(name: String) {
        
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        
        let managedContext = appDelegate.managedObjectContext
        let entity = NSEntityDescription.entityForName("Person", inManagedObjectContext: managedContext)
        
        
        let person = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)
        person.setValue(name, forKey: "name")
        
        do {
            try managedContext.save()
        }catch {
            print("Error")
        }
        
        people.append(person)
        
    }


Core Data 읽기 데이터

    override func viewWillAppear(animated: Bool) {
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let managedContext = appDelegate.managedObjectContext
        
        let fetchRequest = NSFetchRequest(entityName: "Person")
        
        var fetchRequests = [NSManagedObject]()
        
        do {
            fetchRequests = try managedContext.executeFetchRequest(fetchRequest) as! [NSManagedObject]
        }catch {
            
        }
        
        self.people = fetchRequests
        
        
    }``` 

좋은 웹페이지 즐겨찾기