How to remove a SwiftyJSON element?

被刻印的时光 ゝ 提交于 2019-12-23 16:04:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!