我有这样的字典
seconds | healthy | events
0 | ✔ | * liveness probe healthy
1 | ✔ | - SIGTERM
2 | ✔ |
3 | ✔ |
4 | ✔ |
5 | ✔ | * liveness probe unhealthy (1/3)
6 | ✔ |
7 | ✔ |
8 | ✔ |
9 | ✔ |
10 | ✔ | * liveness probe unhealthy (2/3)
11 | ✔ |
12 | ✔ |
13 | ✔ |
14 | ✔ |
15 | ✘ | * liveness probe unhealthy (3/3)
.. | ✔ | * traffic is served
28 | ✔ | * traffic is served
29 | ✘ | * pod restarts
我从另一个ViewController获得了student_id,因此我需要像这样附加它们。
var students = ["student_id":2,"present":0]
我如何实现这一目标
答案 0 :(得分:2)
您无法在Swift中创建具有相同键名的字典。如果您尝试执行此操作,则每次都将替换student_id
和present
值。
相反,您可以创建如下的字典记录数组。
var students: [[String : Int]] = [["student_id": 2, "present": 0]]
let ids = [49,29,12,4]
let anotherRecords = ids.map { ["student_id": $0, "present": 0] }
students.append(contentsOf: anotherRecords)
print(students)
您得到了这样的输出。
[["student_id": 2, "present": 0],
["student_id": 49, "present": 0],
["present": 0, "student_id": 29],
["student_id": 12, "present": 0],
["present": 0, "student_id": 4]]
答案 1 :(得分:1)
您不能在存储相同键“ student_id”的地方使用字典。 ID对于字典应该是唯一的。 除了这个,你可以这样存储
var students =[2:0]
students[49] = 0
students[29] = 0
students[12]= 0
students[4]= 0
答案 2 :(得分:1)
要首先将值附加到字典,您需要创建一个如下所示的字典 然后在其中附加值
var students = [["student_id":2,"present":0]]
students.append(["student_id":49,"present":0])