【发布时间】:2020-02-24 08:24:42
【问题描述】:
这是我拥有的 json 结构...
{
"comment": "Test123",
"scheduleId": "291701",
"observations": [
{
"typeId": 1,
"comment": "Test",
"categoryId": 852,
"smateeId": 217094,
"subCategoryId": 5726,
"hasImage": false
},
{
"typeId": 2,
"categoryId": 861,
"comment": "Hshh",
"subCategoryId": 5825,
"smateeId": 217094,
"hasImage": false
},
{
"subCategoryId": 5908,
"smateeId": 217094,
"typeId": 3,
"comment": "Hehh",
"categoryId": 870,
"hasImage": false
}
],
"schedule": {
"smatorIds": [
19369,
19370
],
"workstationId": "196890",
"plant": {
"id": "2980",
"gaiaCode": "k987976",
"name": "ASDHKJK/ASDA"
},
"departmentId": "188848"
},
"shiftId": null
}
现在我想要实现的是从observations 数组中删除一个特定元素。在observations 数组中,键typeId、categoryId、smateeId、subCategoryId 不是唯一的。所以为了有一个唯一的值来识别每个对象,我像这样在字典中添加了一个随机数......
ArrayData.shared.obsIndex = Int.random(in: 0 ..< 10000)
positiveObservationDict["smateeId"] = ArrayData.shared.smatReceiverIDArray1[0]
positiveObservationDict["categoryId"] = Int(self.Category_Id)
positiveObservationDict["typeId"] = 1
positiveObservationDict["subCategoryId"] = Int(self.Sub_Category_Id)
positiveObservationDict["comment"] = self.PO_Comments.text!
positiveObservationDict["hasImage"] = false
positiveObservationDict["tempID"] = ArrayData.shared.obsIndex
ArrayData.shared.arrayOfObservations.append(positiveObservationDict)
现在我将另一个字典两次放入同一个数组中......(我再次将一个随机数也添加到该字典中)
ArrayData.shared.obsIndex = Int.random(in: 0 ..< 10000)
newDict["smateeId"] = ArrayData.shared.smatReceiverIDArray1[0]
newDict["categoryId"] = Int(self.Category_Id)
newDict["typeId"] = 1
newDict["subCategoryId"] = Int(self.Sub_Category_Id)
newDict["comment"] = self.PO_Comments.text!
newDict["hasImage"] = false
newDict["tempID"] = ArrayData.shared.obsIndex
ArrayData.shared.arrayOfObservations.append(newDict)
所以我添加随机数后的新json结构是这样的……
{
"comment": "Test123",
"scheduleId": "291701",
"observations": [
{
"typeId": 1,
"comment": "Test",
"categoryId": 852,
"smateeId": 217094,
"subCategoryId": 5726,
"hasImage": false
“tempID”: 2563
},
{
"typeId": 2,
"categoryId": 861,
"comment": "Hshh",
"subCategoryId": 5825,
"smateeId": 217094,
"hasImage": false
“tempID”: 765
},
{
"subCategoryId": 5908,
"smateeId": 217094,
"typeId": 3,
"comment": "Hehh",
"categoryId": 870,
"hasImage": false
“tempID”: 174
}
],
"schedule": {
"smatorIds": [
19369,
19370
],
"workstationId": "196890",
"plant": {
"id": "2980",
"gaiaCode": "k987976",
"name": "ASDHKJK/ASDA"
},
"departmentId": "188848"
},
"shiftId": null
}
现在我想用tempID 765 删除对象。这就是我正在做的事情......
let index = ArrayData.shared.arrayOfObservations.index(where: { dictionary in
guard let value = dictionary["tempID"] as? Int
else { return false }
return value == ArrayData.shared.obsIndex
})
if let index = index {
ArrayData.shared.arrayOfObservations.remove(at: index)
}
但是通过这样做,被删除的对象是带有tempID 2563 的对象,而不是带有tempID 765 的对象。我怎样才能删除正确的对象..?
编辑:另外,在return value == ArrayData.shared.obsIndex 上面的这个地方,我也不能在return value == 765 上做这样的事情。我想传递确切的值...
【问题讨论】:
-
尝试将
return value == ArrayData.shared.obsIndex更改为return value == 765 -
您可能不想对索引使用随机数,因为您最终可能会得到重复的 id,这可能会导致难以追踪问题。你能改用自动递增的整数吗?
-
我不能硬编码为
return value == 765@JoakimDanielson,因为我可能在不同的时间得到不同的ID。我要传递我要删除的 id.. -
用变量替换 765 应该很简单
标签: ios swift dictionary