将领域数据存储在iOS中的自定义数据类型数组中

时间:2019-03-07 14:00:43

标签: swift realm

我已将我的API响应存储在领域对象模型中。现在,当我从模型中检索数据时,我想在自定义数据类型数组中存储一些变量,然后将剩余的数据和数组存储在一个数组中。如何做到这一点?这就是我存储数据的方式,

do {
        let realm = try! Realm()
        self.realm = realm
        try! realm.write {

            let newListing = SingleChatRealm()

            for items in dateWiseSortedSingleRooms
            {
                newListing.actualNameFor_1_2_1_chat = items.actualNameFor_1_2_1_chat
                newListing.isGroup = items.isGroup
                newListing.isNewGroup = items.isNewGroup
                newListing.lastMessage = items.lastMessage
                newListing.lastMsgRead = items.lastMsgRead
                newListing.lastMsgTime = items.lastMsgTime
                newListing.lastMsgTimeActual = items.lastMsgTimeActual
                for item in items.members
                {
                    newListing.sid = item.sid!
                    newListing.lastConsumedMessageIndex = item.lastConsumedMessageIndex as? Int ?? 0
                    newListing.lastConsumptionTimestamp = item.lastConsumptionTimestamp ?? ""
                    newListing.identity = item.identity!
                    if item.lastConsumptionTimestampAsDate == nil
                    {
                        let currentDate = Date()
                        newListing.lastConsumptionTimestampAsDate = currentDate
                    }
                    else
                    {
                        newListing.lastConsumptionTimestampAsDate = item.lastConsumptionTimestampAsDate!
                    }
                }
                for item in items.messages
                {
                    newListing.sid = item.sid!
                    newListing.author = item.author!
                    newListing.body = item.body ?? ""
                    newListing.dateUpdated = item.dateUpdated!
                    newListing.dateUpdatedAsDate = item.dateUpdatedAsDate!
                    newListing.index = Int(truncating: item.index!)
                    newListing.lastUpdatedBy = item.lastUpdatedBy ?? ""
                    newListing.mediaFilename = item.mediaFilename ?? ""
                    newListing.mediaSid = item.mediaSid ?? ""
                    newListing.mediaSize = Int(item.mediaSize)
                    newListing.mediaType = item.mediaType ?? ""
                    newListing.messageSid = item.sid!
                    newListing.timestamp = item.timestamp!
                    newListing.timestampAsDate = item.timestampAsDate!
                    newListing.group_info = items.group_info

                }
                newListing.name = items.name
                newListing.profilePic = items.profilePic ?? ""
                newListing.roomSID = items.roomSID
                newListing.unReadMsgsCount = items.unReadMsgsCount
                print(newListing)
                self.realm.add(newListing)
            }
            }

        } catch let error as NSError {
                // handle error
            print(error)
        }

这就是我从模型中获取数据的方式,

 let realm = try! Realm()
    guard let info = realm.objects(SingleChatRealm.self).first else {return}

    for items in localSingleChatRealmArray
    {
        items.actualNameFor_1_2_1_chat = info.actualNameFor_1_2_1_chat
        items.isGroup = info.isGroup
        items.isNewGroup = info.isNewGroup
        items.lastMessage = info.lastMessage
        items.lastMsgRead = info.lastMsgRead
        items.lastMsgTime = info.lastMsgTime
        items.lastMsgTimeActual = info.lastMsgTimeActual
        items.sid = info.sid
        items.lastConsumedMessageIndex = info.lastConsumedMessageIndex
        items.lastConsumptionTimestamp = info.lastConsumptionTimestamp
        items.identity = info.identity
        items.lastConsumptionTimestampAsDate = info.lastConsumptionTimestampAsDate
        items.sid = info.sid
        items.author = info.author
        items.body = info.body
        items.dateUpdated = info.dateUpdated
        items.dateUpdatedAsDate = info.dateUpdatedAsDate
        items.index = info.index
        items.lastUpdatedBy = info.lastUpdatedBy
        items.mediaFilename = info.mediaFilename
        items.mediaSid = info.mediaSid
        items.mediaSize = Int(info.mediaSize)
        items.mediaType = info.mediaType
        items.messageSid = info.sid
        items.timestamp = info.timestamp
        items.timestampAsDate = info.timestampAsDate
        items.group_info = info.group_info
        items.name = info.name
        items.profilePic = info.profilePic
        items.roomSID = info.roomSID
        items.unReadMsgsCount = info.unReadMsgsCount
    }

现在我要存储这些值,

items.sid = info.sid
        items.lastConsumedMessageIndex = info.lastConsumedMessageIndex
        items.lastConsumptionTimestamp = info.lastConsumptionTimestamp
        items.identity = info.identity
        items.lastConsumptionTimestampAsDate = info.lastConsumptionTimestampAsDate 

在一个成员= TCHMember的数组中,此后我想将所有数据和该成员数组存储在一个通用数组中,该怎么做?

0 个答案:

没有答案