问题
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?
回答1:
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)
回答2:
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)
来源:https://stackoverflow.com/questions/34836400/how-to-remove-a-swiftyjson-element