Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 634
Joe Huang Avatar asked Dec 09 '22 00:12

Joe Huang


1 Answers

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)
like image 167
Joe Huang Avatar answered Dec 24 '22 06:12

Joe Huang