我正在尝试使用 json 和 api 从我的网络服务器获取数据,并将它们放入我的应用程序中,服务器中的数据是重复的,我想删除重复的数据,所以现在这是我的数据类,它称为 TypeItems 类
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
}
}
这是我的主要课程
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!")
}
}
}
}
现在我只想从列表中删除重复的项目,我想按 car_type 和类型和型号排序,如果项目重复,我想删除它
