2016-12-18 6 views
2

내 coreData에 28 개의 항목이있는 배열을 저장하고 싶습니다. 이 일을 할 수있는 방법이 있습니까? 이 코드로 시도했지만이 코드는 값을 다시 쓰는 것만 같습니다.Swift Array in CoreData

let appDelegate = 
     UIApplication.shared.delegate as! AppDelegate 

    let managedContext = appDelegate.persistentContainer.viewContext 

    let entity = NSEntityDescription.entity(forEntityName: "PillTook", 
              in:managedContext) 

    let value = NSManagedObject(entity: entity!, 
           insertInto: managedContext) 

    for var index in 0...27 { 

     value.setValue(false, forKey: "took") 

     do { 
      try managedContext.save() 

      pillTook.append(value) 

     } catch let error as NSError { 
      print("Could not save \(error), \(error.userInfo)") 
     } 
    } 

답변

0

예, 코드는 하나의 NSManagedObject 생성하고 for 루프에서의 took 속성의 값을 업데이트합니다. let value = ... 행을 for 루프로 이동하여 각 반복마다 NSManagedObject을 새로 만듭니다. 또한 각 반복마다 저장하는 대신 for 루프 다음에 한 번만 컨텍스트를 저장하는 것이 좋습니다.

+1

감사합니다. :) –