'Swift UITableView with a datasouce of different data types

From the JSON file, I create a model. It has several different types. In my view I have a segmentedControl, where user can choose a type and filter the tableView accordingly.

private enum Section {
    case all(???)
    case case1(items: [type1])
    case case2(items: [type2])
    case case3(items: [type3])
}

    switch section {
    case .type1(let items):
        setupCell(items)
    case .type2(let items):
        setupCell(items)
    case .type3(let items):
        setupCell(items)
    }

The problem appears if I want to have a segment All and show all 3 types together. It means I would have an array of type Any [type1, type2, type3] and need to cast every type. What would be the be approach to handle this use-case? Create a protocol that every type should conform? Is there some better approach?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source