如何编写面向协议的部分

时间:2018-04-05 18:53:28

标签: ios swift uitableview protocols associated-types

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

我的tableview有不同的部分类型。

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之间的差异)一个解决方案就是{ {1}}然后退回,但这不是一个好主意。

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 } } 无用,它只适用于 SomeSection

CustomObject1

我该如何解决这个问题?

修改

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

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

现在我必须将其更改为:

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

}

正确?

现在问题是Rich建议的方法是我不能将这两个Generic类的实例class ViewController<Row>: UIViewController{ var dataSource : [Section<Row>] = [] } 放到一个数组中,因为它们的泛型属性类型最终不一样。

1 个答案:

答案 0 :(得分:4)

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

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
  }
}

如果不要求类实现任何其他功能

,那可能会更好