我试图在JournalEntryVC.swift中添加与患者相关的不同日记条目。
我的病人类有一个日记条目数组,即let journalEntries = List<Journal>()我的日记类有表示日记条目属性的属性,即entryTypeentryDateentryCaption
但是,在JournalEntryVC.swift中循环json时,不仅向日记条目数组附加新的日记条目对象,而且以前附加的日记条目属性也被最新的日记条目属性替换。
例如,newPatient.journalEntries.append(myJournal)newPatient.journalEntries[0]entryCaption的字符串值与entryType相同,以此类推。
主要问题:对于如何添加新的日记账分录及其属性,但不更改以前附加日记账分录的日记账分录属性,有何想法?
谢谢您!

//  Patient.swift

import Foundation
import RealmSwift


class Patient: Object {

    dynamic var patientName = ""
    dynamic var patientAvatar = ""
    dynamic var patientId = 0
    dynamic var isDietitian = false

//array of journal entries for each patient
    let journalEntries = List<Journal>()


    override class func primaryKey() -> String {
        return "patientId"
    }
}

//  Journal.swift

import Foundation
import RealmSwift


class Journal: Object {

    dynamic var entryId = ""
    dynamic var entryType = ""
    dynamic var entryImg = ""
    dynamic var entryCaption = ""
    dynamic var entryComment = ""
    dynamic var entryCategory = ""
    dynamic var entryDate = ""


    override class func primaryKey() -> String {
        return "entryId"
    }

}

//JournalEntryVC.swift

import UIKit
import Alamofire
import BXProgressHUD
import SwiftyJSON
import RealmSwift


class JournalEntryVC: UIViewController, UITableViewDelegate, UITableViewDataSource {


  func reloadInitialData ( ) {

    //mutable arrays containing journal entry attributes
    desc.removeAllObjects()
    type.removeAllObjects()
    category.removeAllObjects()
    metric_stat.removeAllObjects()
    entryImages.removeAllObjects()
    dateCreate.removeAllObjects()
    comments.removeAllObjects()
    entryType.removeAllObjects()
    id.removeAllObjects()
    //comments.removeAllObjects()
    content.removeAllObjects()
    patName.removeAllObjects()


    let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
    dispatch_async(dispatch_get_global_queue(priority, 0)) {
        let request = Alamofire.request(.GET, "https://gethealthie.com/entries.json", headers: [
            "access-token": userCreds.objectForKey("access-token")! as! String,
            "client": userCreds.objectForKey("client")! as! String,
            "token-type": userCreds.objectForKey("token-type")! as! String,
            "uid": userCreds.objectForKey("uid")! as! String,
            "expiry": userCreds.objectForKey("expiry")! as! String
            ]).responseJSON { response in

                print(response.response)

                let json = JSON(data: response.data!)


                if json.count == 0 {
                    BXProgressHUD.hideHUDForView(self.view);
                }else {

                    //Realm- create object instances of Patient and Journal class
                    let newPatient = Patient()
                    let myJournal = Journal()

                    for i in 0 ..< json.count {

                        let entry = json[i]
                        print(entry)

                        let name = entry["entry_comments"]
                        let k = name["id"]

                        //add entry attributes to mutable arrays
                        self.type.addObject(entry["type"].stringValue)
                        self.desc.addObject(entry["description"].stringValue)
                        self.category.addObject(entry["category"].stringValue)
                        self.metric_stat.addObject(entry["metric_stat"].stringValue)
                        self.dateCreate.addObject(entry["created_at"].stringValue)
                        self.comments.addObject(entry["entry_comments"].rawValue)
                        self.entryType.addObject(entry["category"].stringValue)
                        let posterInfo = entry["poster"]
                        let first = posterInfo["first_name"].stringValue
                        let last = posterInfo["last_name"].stringValue
                        let full = first + " " + last
                        self.patName.addObject(full)
                        self.id.addObject(entry["id"].stringValue)
                        self.entryImages.addObject(entry["image_url"].stringValue)


                        //Realm- access properties in Journal class and set it equal to the current journal entry attribute i.e. json[i]
                        myJournal.entryType = entry["type"].stringValue
                        myJournal.entryCaption = entry["description"].stringValue
                        myJournal.entryCategory = entry["category"].stringValue
                        myJournal.metricStat = entry["metric_stat"].stringValue
                        myJournal.entryDate = entry["created_at"].stringValue
                        //  myJournal.entryComment = entry["entry_comments"].rawValue as! String

                        //append a NEW journal entry to the array of journal entries in Patient class i.e. let journalEntries = List<Journal>() as the json loops thru different journal entries
                        newPatient.journalEntries.append(myJournal)

                        if i == json.count - 1 {
                            // do some task
                            dispatch_async(dispatch_get_main_queue()) {
                                self.tableView.reloadData()
                                BXProgressHUD.hideHUDForView(self.view)
                            }
                        }

                    }

                    //Realm- add newPatient object instance to realm
                    try! self.realm.write {
                        self.realm.add(newPatient)

                    }
                }

        }
    }
  }

}

最佳答案

在for循环外实例化Journal的单个实例,更改该Journal的值,然后将其附加到患者的journalEntries。尝试在for循环中移动let myJournal = Journal()

关于ios - Realm -更新对象数组中的所有项目,而不是仅添加新项目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38690434/

10-12 02:46