创建新的域列表并添加项

时间:2016-09-23 03:55:57

标签: json swift realm realm-list

我使用API发出Alamofire个请求,然后我以JSON格式收到回复,然后我将JSON解析为NSDictionary以获取我想要的数据。 我得到的数据是四个Arrays个不同的项目。 我想在Realm中创建一个新的List来保存这些项目。 这是我的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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我建议使用AlamofireObjectMapper的组合来处理所有JSON映射(双向)https://github.com/tristanhimmelman/AlamofireObjectMapper

和在ObjectMapper + Realm https://github.com/Jakenberg/ObjectMapper-Realm

中找到的ListTransform

它们都可以通过cocoapods安装。您的代码看起来更清晰,更易于维护