0

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

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 个数组时得到的结果

在此处输入图像描述

4

2 回答 2

1

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

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

于 2016-09-27T18:04:14.893 回答
0

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

以及在 ObjectMapper+Realm https://github.com/Jakenberg/ObjectMapper-Realm中找到的 ListTransform

它们都可以通过 cocoapods 安装。你的代码看起来会更干净,更容易维护

于 2016-10-27T00:51:39.603 回答