iosswiftswifty-json

How to remove a SwiftyJSON element?


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?


Solution

  • 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)