2016-04-01 7 views
1

私はSwiftを初めて使用しています。私は機能を実装する方法について心配しています。ユーザーが選択して選択を解除して配列に保存する必要がある場合は、複数の選択肢があるテーブルビューがあり、配列のレコードは後でコアデータに保存されます。私は多くの方法を試しましたが、1つのレコードしか保存していません。複数のレコードをコアデータに保存する方法

+0

私の推測:* single *レコードのみを作成し、同じレコードを複数回変更して保存します。しかし、あなたのコードを見ることなく、その質問に答えることは不可能です。 –

+0

あなたのコードを教えてください。 –

答えて

2

レコードごとに配列を走査し、新しい[NSEntityDescription insertNewObjectForEntityForName]オブジェクトを作成する必要があります。以下は、coreDataEntityNameとレコードの配列を受け入れる一般的なメソッドである例です。あなたのエンティティ名が、属性として名前を持つRecordであるとし、配列から属性を設定してコンテキストを保存することができます。

func insertItems(coreDataTable : String, records: NSArray) -> Bool { 
     let error: NSError? 
     for(item in records as NSDictionary!) { 
      let entity = NSEntityDescription.entityForName(coreDataTable, inManagedObjectContext: self.managedContext) 
      var record = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext) as Record! 
      record.name = item["name"] 
      if (!self.mainObjectContext.save()) { 
       // Replace this implementation with code to handle the error appropriately. 
       // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
       NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
       //abort(); 
       return false 
      } 
     } 
    return true 
} 

これですべてのレコードが追加されます。

+0

このコードは複数の構文エラーを投げています。親切にチェックアウト – amish

関連する問題