如何编写面向协议的部分

How to write protocol oriented sections

这只是我真实示例的模型,我的行是复杂的对象

我的表格视图有不同的部分类型。

enum Type{
    case devices
    case users
    case status
}

显然每个部分都有一些行,可能有一个 headerTitle 和一个 sectionType,我已经尽可能地概括了这一点。不确定使用 associatedType 是否是正确的方法......可能有一个更简单的解决方案只使用协议

protocol SectionType{
    associatedtype Section
    associatedtype Rows

    init(sectionType: Section, rows: Rows)

    var sectionType: Section {get set}
    var rows: Rows  {get set}
    var headerTitle: String? {get set}
}

主要问题是每个部分的行可以完全不同(超过customObject1customObject2之间的差异)一种解决方案就是做 var rows: Any 然后回退,但这不是一个好主意。

class CustomObject1{
    var number: Int

}

class CustomObject2{
    var name : String?

}

我对协议的遵守情况:

class SomeSection: SectionType{

    var sectionType: Type
    var rows: [CustomObject1]
    var headerTitle: String?

    required init(sectionType: Type, rows: [CustomObject1]){
        self.sectionType = sectionType
        self.rows = rows
    }
}

如你所见,我的SomeSectionclass没用,只对 CustomObject1

var dataSource : [SectionType] = []

let firstSection = SomeSection(sectionType: .devices, rows: [CustomObject1(), CustomObject1(),CustomObject1()])
let secondSection = SomeSection(.users, rows: [???????]) // I can't add `CustomObject2` instances...nor I think creating a new class is a good idea


dataSource.append(firstSection)
dataSource.append(secondSection)
tableview.datasource = dataSource

我该如何解决这个问题?


编辑

我认为我的协议方法完全没有必要。但是现在我有一个不同的问题。我的 viewController 以前是这样的:

class ViewController: UIViewController{
var dataSource : [Section] = []

}

现在我必须将其更改为:

class ViewController<Row>: UIViewController{
var dataSource : [Section<Row>] = []

}

对吗?

Rich 建议的方法现在的问题是我不能 apppend 通用 class 的这两个实例到一个数组中,因为它们的通用属性类型最终是不一样。

我认为您可以针对此用例使用通用 class 来简化解决方案,但如果您想使用协议和关联类型,则以下方法应该有效:

protocol SectionType {
  associatedtype Section
  associatedtype Row

  init(sectionType: Section, rows: [Row]) 
  var sectionType: Section {get set}
  var rows: [Row]  {get set}
  var headerTitle: String? {get set}
}

class SomeSection<T,U>: SectionType{
  typealias Section = T
  typealias Row = U

  var sectionType: Section
  var rows: [Row]
  var headerTitle: String?

  required init(sectionType: Section, rows: [Row]){
    self.sectionType = sectionType
    self.rows = rows
  }
}

enum FoodType {
  case cheese
  case beer
}

enum Currency {
  case dollars
  case pounds
}

let s1 = SomeSection(sectionType: FoodType.cheese, rows: ["cheddar", "stilton"])
let s2 = SomeSection(sectionType: FoodType.beer, rows: ["rochefort12", "Spring Sprinter"])
let s3 = SomeSection(sectionType: Currency.dollars, rows: [1,2])

通用版本只是:

class SomeSection<Section,Row> {
  var sectionType: Section
  var rows: [Row]
  var headerTitle: String?

  required init(sectionType: Section, rows: [Row]){
    self.sectionType = sectionType
    self.rows = rows
  }
}

如果 class 不需要实现任何其他功能,这可能会更好