How to remove a SwiftyJSON element?
Asked Answered
L

3

5

I have a JSON array (say, dataObj) generated by SwiftyJSON and I try to remove its element like this:

let count=dataObj.count
for var m=x; m<count; ++m {
    dataObj[m] = nil  // there is no removeAtIndex() somehow
}

print(dataObj.count)
print(dataObj)

After execution, dataObj.count remains the same and print shows now dataObj becomes

[null, null, null, ... ]

What's the way to really remove an element for SwiftyJSON?

Lehmann answered 17/1, 2016 at 8:14 Comment(8)
dataObj.removeAtIndex[m] or dataObj.removeAll() ... etc.Asterisk
Removing an object in a repeat loop by (increasing) index is a very bad idea. Assuming you have two elements and you have just removed object[0]. Then object[1] becomes object[0] and there is no index 1 anymore. That causes a pretty out of bounds exception. The only (safe) way to remove by index in a repeat loop is removing backwards.Jann
@Jann Thanks I will remove backwards.Lehmann
@Asterisk If I can use that directly, I will not ask this question.Lehmann
why you can't? by the way, if you want to remove all elements, why not to use removeAll() ?? as sad by vadian: "The only (safe) way to remove by index in a repeat loop is removing backwards" is true if you will remove the last element only. in that case you can use while dataObj.count > 0 { dataObj.removeLast() } or (without using index) while dataObj.count > 0 { dataObj.removeFirst() }.Asterisk
... (continue) so, simple use removeAll(). if you have some other criteria for remove / not remove inside the loop, use filter instead.Asterisk
@user3441732 there is no such method for the returned JSON type by SwiftyJSON. Although it's like an array but it has no such a method.Lehmann
@EricD. Thanks but I found the answer. I thought about making Swift objects but in that case, I rather give up using SwiftyJSON. I don't want to do the extra copying if the library does not allow me to alter data while I am trying to do extra parsing.Lehmann
L
11

Finally I found the answer to remove an element in JSON (array type) created by SwiftyJSON:

dataObj.arrayObject?.removeAtIndex(m)

By the way, to remove an element when JSON returned by SwiftyJSON in a dictionary type:

jsonObj.dictionaryObject?.removeValueForKey(keyValue)

Update

Swift 3+ -

Array:

dataObj.arrayObject?.remove(at: m)

Dictionary:

jsonObj.dictionaryObject?.removeValue(forKey: keyValue)
Lehmann answered 17/1, 2016 at 13:52 Comment(0)
U
5

Update on Joe's answer for Swift 4.

Removing a dictionary element from JSON:

json.dictionaryObject?.removeValue(forKey: key)

Removing an array element from JSON:

json.arrayObject?.remove(at: index)
Ursal answered 7/5, 2018 at 16:30 Comment(0)
O
0

If you are storing dictionaries, you can remove elements using dictionaryObject.removeValue(forKey:). This mutates the JSON object in place.

For example:

var object = JSON([
    "one": ["color": "blue"],
    "two": ["city": "tokyo",
            "country": "japan",
            "foods": [
                "breakfast": "tea",
                "lunch": "sushi"
                ]
            ]
])

Lets remove the country key:

object["two"].dictionaryObject?.removeValue(forKey: "country")

If you print(object), you'll see that the country key no longer exists.

{
  "one" : {
    "color" : "blue"
  },
  "two" : {
    "city" : "tokyo",
    "foods" : {
      "breakfast" : "tea",
      "lunch" : "sushi"
    }
  }
}

This also works for nested dictionaries:

object["two"]["foods"].dictionaryObject?.removeValue(forKey: "breakfast")
{
  "one" : {
    "color" : "blue"
  },
  "two" : {
    "city" : "tokyo",
    "foods" : {
      "lunch" : "sushi"
    }
  }
}
Overspill answered 10/8, 2023 at 14:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.