0

Kinvey をバックエンドとして使用してアプリケーションを開発しています。そして、、、およびプロパティQuestionsで構成されるデータベースに格納する必要があるオブジェクトの配列があります。私の問題は、この質問の配列をデータベースに保存しようとすると、配列の最後のオブジェクトが常に n 回保存されることです。QnumberQanswer1Qanswer2

これらのデータが配列に格納されているとします。

Questions[0].Qnumber = 1
Questions[0].Qanswer1 = "a1"
Questions[0].Qanswer2 = "b1"

Questions[1].Qnumber = 2
Questions[1].Qanswer1 = "a2"
Questions[1].Qanswer2 = "b2"

Questions[2].Qnumber = 3
Questions[2].Qanswer1 = "a3"
Questions[2].Qanswer2 = "b3"

ループを使用してこれらのオブジェクトをバックエンドに保存し、データベースをチェックすると、次のようになります。

Qnumber : 3 , 3 , 3 Qanswer1 : a3, a3 , a3 Qanswer2 : b3, b3, b3

これを解決するために2日間費やしましたが、できませんでした。これが私のコードです:

@IBAction func saveButton(sender: AnyObject) {

    let i = Int(numberOfQuestions)
    var newQ : Questions = Questions()
    for var index = 0; index < i; ++index {
        newQ.Qnumber = String(index + 1)
        newQ.Qanswer1 = cellAnswer1[index]
        newQ.Qanswer2 = cellAnswer2[index]
        saveObject(newQ)
    }
}

func saveObject (newQuestion: Questions){
    let store = KCSAppdataStore.storeWithOptions([
        KCSStoreKeyCollectionName : "Questions",
        KCSStoreKeyCollectionTemplateClass : Questions.self
        ])


    store.saveObject(
        newQuestion,
        withCompletionBlock: { (objectsOrNil: [AnyObject]!, errorOrNil: NSError!) -> Void in
            if errorOrNil != nil {
                NSLog("an error happened: %@", errorOrNil)
            } else {
                //save was successful
                NSLog("A new question is saved sucessfully")
            }
        },
        withProgressBlock: nil
    )
}
4

1 に答える 1