gpt4 book ai didi

SwiftyJSON : Remove all object from an array

转载 作者:行者123 更新时间:2023-11-28 12:27:04 24 4
gpt4 key购买 nike

我正在尝试使用 removeAll() 清除由 SwiftyJSON 创建的数组中的所有项目,但它不起作用。我做错了什么?

let itemsArray = ["One", "Two"]
var jsonObject = JSON(itemsArray)
jsonObject.arrayObject?.removeAll()
let item = jsonObject.arrayValue

最佳答案

你不能修改 JSON 的数组:

解决方案

清理阵列的唯一选择:

jsonObject = JSON([String]())
//jsonObject = JSON([Any]())

完整样本

let itemsArray = ["One", "Two", "Three"]
var tempArray = itemsArray
var jsonObject = JSON(itemsArray)

let array: [String]? = []
jsonObject.arrayObject = array
print("===========================================")
print("Original array:\n\(jsonObject)")
//jsonObject = JSON([Any]())
jsonObject = JSON([String]())
print("Empty JSON array:\n\(jsonObject)")
print("===========================================")

// Not working
print("\nNot working exaple 1:")
jsonObject = JSON(itemsArray)
jsonObject.arrayObject?.removeAll()
print("JSON: jsonObject.arrayObject?.removeAll() = \n\(jsonObject)")
tempArray.removeAll()
print("!!! But [string].removeAll() = \(tempArray)")
print("===========================================")

print("\nNot working exaple 2:")
tempArray = itemsArray
for _ in 0..<jsonObject.arrayObject!.count {
jsonObject.arrayObject!.removeLast()
tempArray.removeLast()
}

print("JSON: for ... { jsonObject.arrayObject!.removeLast() } = \n\(jsonObject)")
print("!!! But [String]:for ...{ tempArray.removeLast() } = \(tempArray)")

结果

enter image description here

关于SwiftyJSON : Remove all object from an array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43101298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com