I am having a problem that I can't find a good answer to by myself.
I have an array containing objects that contain information about companies. Based on this objects I create a collection view where I populate the items from the information inside those objects.
var suppliers = [Suppliers]()
Now I want to add buttons with specific filters. When pressed on one or more of those buttons the collection view should update with the filters applied.
Therefore I made a few buttons and when pressed I add them to an array containing all the filters.
var filters = [Category]()
After that I call a method that should do the filtering and update the collection view.
private func filter()
{
if filters.count > 0 {
suppliers.filter({ ($0.category!).contains(filters)})
}
}
Unfortunately I can't comparison like that, according to xCode. How do I achieve this filtering correctly?
edit: See the structure of the object below.
struct Supplier : JSONJoy {
var about: String?
var category: Array<Category>?
var logo: String?
var name: String?
init(_ decoder: JSONDecoder) {
name = decoder["name"].string
logo = decoder["logo"].string
about = decoder["about"].string
if let ctgrys = decoder["category"].array {
category = Array<Category>()
for ctgryDecoder in ctgrys {
category?.append(Category(ctgryDecoder))
}
}
}
struct Category : JSONJoy {
var id: Int?
var name: String?
init(_ decoder: JSONDecoder) {
id = decoder["id"].integer
name = decoder["name"].string
}
}