我的数组中的最后一个元素覆盖所有以前的元素

时间:2017-11-03 14:58:59

标签: arrays swift

我正在尝试将数据从JSON保存到我的数组中,但最后一项会覆盖以前的值。我有一个用于存储学生信息的学生课程和用于存储学生技能的技能课程。

public class Skill {
    public var id : Int = 0
    public var name : String = ""
    public var level : Double = 0.0

    func description() -> String {
        return "ID: \(id), " +
            "name: \(name), " +
        "level: \(level)"
    }
}

学生班

public class Student {

    public var studentSkills = Array<Skill>()


    func setCursusUsers(data: [[String: AnyObject]]) {
        for item in data {
            let skills = Skill()
            if let resData = item["skills"]  {
                let stu_data = resData as! [[String: AnyObject]]
                for skill_item in stu_data {
                    skills.id = skill_item["id"] as? Int ?? 0
                    skills.level = skill_item["level"] as? Double ?? 0.0
                    skills.name = skill_item["name"] as? String ?? ""
                    studentSkills.append(skills)
                    print(studentSkills[studentSkills.count - 1].description())
                }
                for skill in studentSkills {
                    print(skill.description())
                }
            }
        }
    }
}

我遍历技能数组并将其附加到我的studentSkills数组并打印最后一个附加值并打印出正确的值。

for skill_item in stu_data {
    skills.id = skill_item["id"] as? Int ?? 0
    skills.level = skill_item["level"] as? Double ?? 0.0
    skills.name = skill_item["name"] as? String ?? ""
    studentSkills.append(skills)
    print(studentSkills[studentSkills.count - 1].description())
 }

print循环中上述for的输出

ID: 16, name: Company experience, level: 11.34
ID: 4,  name: Unix, level: 7.4
ID: 17, name: Object-oriented programming, level: 4.59
ID: 3,  name: Rigor, level: 3.24
ID: 7,  name: Group & interpersonal, level: 3.02
ID: 2,  name: Imperative programming, level: 2.98
ID: 6,  name: Web, level: 2.32
ID: 13, name: Organization, level: 1.8
ID: 14, name: Adaptation & creativity, level: 1.14
ID: 5,  name: Graphics, level: 1.12
ID: 10, name: Network & system administration, level: 0.98
ID: 12, name: DB & Data, level: 0.87
ID: 15, name: Technology integration, level: 0.82
ID: 1,  name: Algorithms & AI, level: 0.76
ID: 11, name: Security, level: 0.45

当我在上面的for循环

之外运行以下代码时
for skill in studentSkills {
    print(skill.description())
}

我得到以下输出

ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 11, name: Security, level: 0.45
ID: 7, name: Group & interpersonal, level: 0.21
ID: 7, name: Group & interpersonal, level: 0.21
ID: 7, name: Group & interpersonal, level: 0.21
ID: 7, name: Group & interpersonal, level: 0.21

这是我的JSON

的结构
"cursus_users" : [
  {
    "skills" : [
      ...,
      ...,
      ...,
      ...,
      ...,
      ...,
      ...,
      ...,
      {
        "id" : 11,
        "name" : "Security",
        "level" : 0.45
      }
    ],
    ...
  },
  {
    "skills" : [
      ...,
      ...,
      ...,
      {
        "id" : 7,
        "name" : "Group & interpersonal",
        "level" : 0.21
      }
    ],
    ...
  }
]

技能数组的最后一个元素会覆盖以前的元素,我该如何解决?

1 个答案:

答案 0 :(得分:0)

您总是将相同的技能添加到数组中。每次在for循环中修改它。您应该每次都创建新的Skill

for skill_item in stu_data {
     let skills = Skill()
     ...
}

如果Skill是值类型(struct),您的方法会有效,但它是一个引用类型(class)并指向同一个对象。