gpt4 book ai didi

json - 创建新的 Realm 列表并添加项目

转载 作者:行者123 更新时间:2023-11-28 16:10:17 25 4
gpt4 key购买 nike

我用 Alamofire 发出一个 API 请求,然后我得到一个 JSON 格式的响应,然后我解析 JSONNSDictionary 以获取我想要的数据。我得到的数据是四个不同项目的 Arrays。我想在 Realm 中创建一个新列表来保存这些项目。这是我的 Realm 对象类:

class ListOfDefinitions: Object {
let listOfItems = List<Item>()
}

class Item: Object {

dynamic var AverageCost = Int()
dynamic var Barcode = ""
dynamic var Description = ""
dynamic var InternalUnique = Int()
dynamic var LastCost = Int()
dynamic var LimitToMainRegionUnique = Int()
dynamic var Notes = ""
dynamic var StockCategoryUnique = Int()
dynamic var StockCode = ""
dynamic var StockGroupUnique = Int()
dynamic var UnitDescriptor = ""
}

这是我的代码,说明我如何处理 JSON 响应以及我希望在我的代码中将数据保存在何处。

 var newItemInStockList : ListOfDefinitions!   // declared in the class
let newItemInStock = Item()


.responseJSON { response in



switch response.result {

case .Success(let JSON):
// print("Success with JSON: \(JSON)")

let response = JSON as! NSDictionary



let responseParams = response.objectForKey("ResponseParameters") as! NSDictionary
//print(responseParams)

//let stockItemGroupList = responseParams.objectForKey("StockItemGroupList")

let stockItemList = responseParams.objectForKey("StockItemList") as! NSDictionary
//print(stockItemList)

let listofDefinitions = stockItemList.objectForKey("ListofDefinitions") as! NSArray
print(listofDefinitions.count)


for defJson in listofDefinitions {

print(defJson["Description"])

someString = defJson["Description"] as! String
print(someString)
// Because there are 4 arrays of items this for loop will be red 4 times, each time it is red I want o create a new list and add the items to the list
// This comment area is where I tried to create a new list and then .append the items in it, but it doesn't work.
// let newOne = ListOfDefinitions()
//
//
// try! realm.write{
//
// realm.add(newOne)
// }
// self.newItemInStock.AverageCost = defJson["AverageCost"] as! Int
// self.newItemInStock.Barcode = defJson["Barcode"] as! String
// self.newItemInStock.Description = defJson["Description"] as! String
// self.newItemInStock.InternalUnique = defJson["InternalUnique"] as! Int
// self.newItemInStock.LastCost = defJson["LastCost"] as! Int
// self.newItemInStock.LimitToMainRegionUnique = defJson["LimitToMainRegionUnique"] as! Int
// self.newItemInStock.Notes = defJson["Notes"] as! String
// self.newItemInStock.StockCategoryUnique = defJson["StockCategoryUnique"] as! Int
// self.newItemInStock.StockCode = defJson["StockCode"] as! String
// self.newItemInStock.StockGroupUnique = defJson["StockGroupUnique"] as! Int
// self.newItemInStock.UnitDescriptor = defJson["UnitDescriptor"] as! String
//
// try! realm.write{
//
// self.newItemInStockList.listOfItems.append(self.newItemInStock)
// }

}

case .Failure(let error):
print("Request failed with error: \(error)")
}

这是我打印 4 个数组时得到的结果

enter image description here

最佳答案

查看您的示例代码,我认为这里发生的主要问题是您为要添加到列表中的每个对象重新使用相同的 self.newItemInStock 实例。

最好在循环中创建一个新的 Item 对象,并将其附加到 List 对象。

关于json - 创建新的 Realm 列表并添加项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39652336/

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