-1

I'm trying to get data from my web server using json and api, and put them in my app, the data in the server are repeated, I want to remove the repeated data, so now this my data class which called TypeItems class

import Foundation

class TypeItems {
    var car_type:String?
    var type:String?
    var model:String?
    var ph:String?


    init(car_type:String, type:String, model:String, ph:String) {
        self.car_type = car_type
        self.type = type
        self.model = model
        self.ph = ph
    }
}

and thi is my main class

import UIKit

class TypeViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    var Item = Array<TypeItems>()
    var itemsUrl = ""
    var CarType:String?

    @IBOutlet weak var CollectionViewList: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        itemsUrl = "http://mydpmen/reportall.php?car_type=\(CarType!)"
        print(itemsUrl)
        ReadFromUrl(url: itemsUrl)
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return Item.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:TypeCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "typecell", for: indexPath) as! TypeCollectionViewCell

        cell.SetImage(url: Item[indexPath.row].ph!)
        cell.laCarType.text = Item[indexPath.row].car_type! + " " + Item[indexPath.row].type!
        cell.laModel.text = Item[indexPath.row].model

        return cell
    }

    func ReadFromUrl(url:String) {
        DispatchQueue.global().async {

            do {
                let newUrl = url.replacingOccurrences(of: " ", with: "%20")
                let AppUrl = URL(string: newUrl)!
                let data = try Data(contentsOf: AppUrl)
                let json = try JSONSerialization.jsonObject(with: data) as! [[String:Any]]
                print(json)

                DispatchQueue.global().sync {
                    for item in json {

                        let photo = "http://mydomen/photo/Cars/\(item["car_type"]!) \(item["type"]!) \(item["model"]!).png"

                        let newPhoto = photo.replacingOccurrences(of: " ", with: "%20")
                        print("newphoto is \(newPhoto)")

                        self.Item.append(TypeItems(car_type: item["car_type"]! as! String, type: item["type"]! as! String, model: item["model"]! as! String, ph: newPhoto))

                        self.CollectionViewList.reloadData()
                    }
                }
            } catch {
                print("cannot load from server!")
            }
        }
    }
}

this is image of the result

now just I want to remove the repeated items from my list, I want to sort by car_type and type and model, if the item is repeated, I want to remove it

Cœur
  • 37,241
  • 25
  • 195
  • 267
APC Apps
  • 31
  • 3

1 Answers1

0

In ReadFromUrl method, before reloading the collection view write below line.

let objectSet = Set(Item.map { $0.model })

This will create set with an array. Now use this set for collection view data source & delegate.

Note: 1. you can replace model in $0.model with the key which is repeating. 2. Follow naming convention. The variable name starts with lowercase.

pkc456
  • 8,350
  • 38
  • 53
  • 109