附加数组会覆盖Realm对象的最后一个索引

时间:2021-04-10 21:15:45

I have an array of Realm objects and before i save them in Realm DB i have my own array of objects in for loop:

我有一个Realm对象数组,在我将它们保存在Realm DB之前,我在for循环中有自己的对象数组:

var objs = [self.friendsObject] //0 values at first
for i in (0..<json.count) { //counts 2

  let _id = json[i]["_id"] as? String
  let userName = json[i]["userName"] as? String
  let profile_pic = json[i]["profile_pic"] as? String
  let phone = json[i]["phone"] as? String

  self.friendsObject.id = _id!
  self.friendsObject.username = userName!
  self.friendsObject.profilepic = profile_pic!
  self.friendsObject.phone = phone!

  objs.append(self.friendsObject) //2nd element overwrites 1st one
  }
self.friendsObject.save(objects: objs)

So i can see the first object with correct items inside objs before i insert second array, but in second index there are 2 array of objects with same values. i appreciate any help.

所以我在插入第二个数组之前可以在objs中看到第一个具有正确项目的对象,但在第二个索引中有2个具有相同值的对象数组。我感谢任何帮助。

Note: It is not duplicate, i have already checked some similar questions but it doesn't apply to my issue.

注意:它不重复,我已经检查了一些类似的问题,但它不适用于我的问题。

1 个解决方案

#1


2  

As Vadian commented, the problem is that the code is not creating new instances of friendsObject but appending the same instance with different values.

正如Vadian所评论的那样,问题是代码不会创建friendsObject的新实例,而是使用不同的值附加相同的实例。

Edit

Below an example of how to copy JSON to a class based on the information provided in the question:

下面是根据问题中提供的信息将JSON复制到类的示例:

    // Simulating a JSON structure filled with some data.
    var jsonData = [Int: [String: String]]()

    for index in 0..<10 {
        var values = [String: String]()
        values["id"] = "id\(index)"
        values["username"] = "username\(index)"
        values["profilepic"] = "profilepic\(index)"
        values["phone"] = "phone\(index)"
        jsonData[index] = values
    }

    // Friend sample class where JSON data will be copied to.
    class Friend {

        var id: String
        var username: String
        var profilepic: String
        var phone: String

        init(_ id: String, _ username: String, _ profilepic: String, _ phone: String) {
            self.id = id
            self.username = username
            self.profilepic = profilepic
            self.phone = phone
        }
    }

    // The array where to copy the values from the JSON data.
    var friends = [Friend]()

    // Looping through the JSON data with a sorted key.
    for jsonSortedKey in jsonData.keys.sorted(by: <) {

        // Obtaining a JSON element containing friend data.
        let jsonFriend = jsonData[jsonSortedKey]!

        // Creating a new friend's instance from the JSON friend's data.
        let friend = Friend((jsonFriend["id"]!), jsonFriend["username"]!, (jsonFriend["profilepic"]!),  (jsonFriend["phone"]!))

        friends.append(friend)
    }

The result is this:

结果如下:

附加数组会覆盖Realm对象的最后一个索引

#1


2  

As Vadian commented, the problem is that the code is not creating new instances of friendsObject but appending the same instance with different values.

正如Vadian所评论的那样,问题是代码不会创建friendsObject的新实例,而是使用不同的值附加相同的实例。

Edit

Below an example of how to copy JSON to a class based on the information provided in the question:

下面是根据问题中提供的信息将JSON复制到类的示例:

    // Simulating a JSON structure filled with some data.
    var jsonData = [Int: [String: String]]()

    for index in 0..<10 {
        var values = [String: String]()
        values["id"] = "id\(index)"
        values["username"] = "username\(index)"
        values["profilepic"] = "profilepic\(index)"
        values["phone"] = "phone\(index)"
        jsonData[index] = values
    }

    // Friend sample class where JSON data will be copied to.
    class Friend {

        var id: String
        var username: String
        var profilepic: String
        var phone: String

        init(_ id: String, _ username: String, _ profilepic: String, _ phone: String) {
            self.id = id
            self.username = username
            self.profilepic = profilepic
            self.phone = phone
        }
    }

    // The array where to copy the values from the JSON data.
    var friends = [Friend]()

    // Looping through the JSON data with a sorted key.
    for jsonSortedKey in jsonData.keys.sorted(by: <) {

        // Obtaining a JSON element containing friend data.
        let jsonFriend = jsonData[jsonSortedKey]!

        // Creating a new friend's instance from the JSON friend's data.
        let friend = Friend((jsonFriend["id"]!), jsonFriend["username"]!, (jsonFriend["profilepic"]!),  (jsonFriend["phone"]!))

        friends.append(friend)
    }

The result is this:

结果如下:

附加数组会覆盖Realm对象的最后一个索引