解析json表视图时显示重复数据

时间:2019-03-25 05:58:39

标签: arrays json swift

当我在模型中解析json数组并追加数据以在tableview中显示时,它会重复数据

wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz
tar xf Python-3.7.1.tar.xz
cd Python-3.7.1
./configure --enable-optimizations
make -j 8
sudo make altinstall

self.arr由于for循环而重复,但是当我使用 class ProductsModel: Codable { let id, name: String init(id: String, name: String) { self.id = id self.name = name } } if result == "Success" { if let filterResponse = response[APPKEYS.responseData] as? [String: Any] { if let getArrayProducts = filterResponse[APPKEYS.products] as? NSArray { for filterArray in getArrayProducts { if let setFilterArray = filterArray as? [String: Any] { self.arr.append(ProductsModel(id: setFilterArray[APPKEYS.id] as! String, name: setFilterArray[APPKEYS.name] as! String)) self.localizationWork() self.tblView.reloadData() self.hideLoader(); } } } } } 时 一切都正确,但我想附加数据

3 个答案:

答案 0 :(得分:2)

product中附加新的self.arr时添加支票。

代码:

var arr = [ProductsModel]()

if result == "Success" {

    if let filterResponse = response[APPKEYS.responseData] as? [String: Any] {

            if let getArrayProducts = filterResponse[APPKEYS.products] as? NSArray {

                   for filterArray in getArrayProducts {

                        if let setFilterArray = filterArray as? [String: Any] {

                               let product = ProductsModel(id: setFilterArray[APPKEYS.id] as! String, name: setFilterArray[APPKEYS.name] as! String)

                               if let row = self.arr.index(where: {$0.id == product.id}) {

                                    self.arr[row] = product
                               }else{

                                  self.arr.append(product)
                               }


                             }
                        }

                        self.localizationWork()
                        self.tblView.reloadData()
                        self.hideLoader()
                    }
                }
            }

让我知道您是否仍然有任何问题。

答案 1 :(得分:0)

                    if let filterResponse = response[APPKEYS.responseData] as? [String: Any] {
                        if let getArrayProducts = filterResponse[APPKEYS.products] as? NSArray {
                            for filterArray in getArrayProducts {
                                if let setFilterArray = filterArray as? [String: Any] {
                                    self.arr.append(ProductsModel(id: setFilterArray[APPKEYS.id] as! String, name: setFilterArray[APPKEYS.name] as! String))
                                    //self.arr = [ProductsModel(id: setFilterArray[APPKEYS.id] as! String, name: setFilterArray[APPKEYS.name] as! String)]

                                }
                            }
                            self.localizationWork()
                            self.tblView.reloadData()
                            self.hideLoader();
                        }
                    }
                } ```

答案 2 :(得分:0)

尝试一下

// Put this as a property of your class
var products: [String: ProductsModel] = [:]

if result == "Success" {
    if let filterResponse = response[APPKEYS.responseData] as? [String: Any] {
        if let getArrayProducts = filterResponse[APPKEYS.products] as? NSArray {
            for filterArray in getArrayProducts {
                if let setFilterArray = filterArray as? [String: Any] {
                    self.products[setFilterArray[APPKEYS.id] as! String] = ProductsModel(id: setFilterArray[APPKEYS.id] as! String, name: setFilterArray[APPKEYS.name] as! String)
                    self.localizationWork()
                }
            }
            self.tblView.reloadData()
            self.hideLoader();
        }
    }
}