【问题标题】:Not able to delete correct object from json array无法从 json 数组中删除正确的对象
【发布时间】: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 数组中,键typeIdcategoryIdsmateeIdsubCategoryId 不是唯一的。所以为了有一个唯一的值来识别每个对象,我像这样在字典中添加了一个随机数......

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


【解决方案1】:

首先,您需要知道要删除哪些数据的正确信息。您不必拥有唯一的 id 来删除数据。

考虑以下数据

"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
        }]

let observations = [Observation]()
let data = observations.filter({$0.typeId != 1})

此数据将只有“typeId = 2”数据。如果您有多个“typeId = 1”的数据,则所有“typeId = 1”的数据将被删除,其他typeId值将被保留。

【讨论】:

    【解决方案2】:

    语法更简单

    let objectID = 765
    if let index = ArrayData.shared.arrayOfObservations.firstIndex(where: { $0["tempID"] as? Int == objectID}) {
        ArrayData.shared.arrayOfObservations.remove(at: index)
    }
    

    或者写一个函数

    func deleteObject(withID objectID : Int) {
        if let index = ArrayData.shared.arrayOfObservations.firstIndex(where: { $0["tempID"] as? Int == objectID}) {
            ArrayData.shared.arrayOfObservations.remove(at: index)
        }
    }
    

    然后调用它

    deleteObject(withID: 765)
    

    【讨论】:

    • 我不能将值硬编码为765@vadian,因为我可能会在不同的时间得到不同的ID...
    • 然后创建一个要删除的变量 int 并传递给要删除它的函数。
    猜你喜欢
    • 2018-11-13
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    相关资源
    最近更新 更多