Swift generic array does not match type constraint


#1

I am in the process of creating a generic array filter. The dataset is composed of categories and array of items for each category. It is displayed in a tableview which could be filtered by the category.

Why would ‘catFilteredItems’ not know about parameter type T when I append a collection of the same type T. Did I encounter a Swift generics limitation? I was trying to avoid using specific types since I want this filter controller to be generic

class FilterController<T: Identifiable & Filterable>: NSObject {
    var list: ListCollection<T>!
    var filteredList: ListCollection<T>!
    var tabularCollection: [String : [CollectionItem<T>]]!

    init(withList newList: ListCollection<T>, categories: [String]) {
        self.list = newList

        tabularCollection = [String : [CollectionItem<T>]]()

        for cat in categories {
            tabularCollection[cat] = list.list.filter { $0.anObject.category == cat }
        }
    }

    func filter(filters: [String]?) {
        guard let someFilters = filters else {
            filteredList = list
            return
        }

        if someFilters.count < 1 {
            filteredList = list
            return
        }

        var catFilteredItems = [CollectionItem<T>]()

        for catFilter in someFilters {
            guard let filtered = tabularCollection[catFilter] else {
                return
            }
            // ERROR HERE
            // Cannot convert value of type '[CollectionItem<T>]' to expected argument type 'CollectionItem<_>'
            catFilteredItems.append(filtered)
        }

        let sorted = catFilteredItems.sorted(by: { $0.anObject.name > $1.anObject.name })
        filteredList = ListCollection<T>()
        filteredList.list = sorted
    }
}

#2

Instead of

catFilteredItems.append(filtered)

It should have been

catFilteredItems.append(contentsOf: filtered)

Posting this becomes easier to solve. Hahahaha