这只是我真实示例的模型,我的行是复杂的对象
我的表格 View 有不同的部分类型。
enum Type{
case devices
case users
case status
}
显然每个部分都有一些行,可能有一个 headerTitle 和一个 sectionType,我已经尽可能地概括了这一点。不确定使用 associatedType
是否是正确的方法......可能有一个更简单的解决方案,只使用协议(protocol)
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}
}
主要问题是每个部分的行可以完全不同(超过customObject1
和customObject2
之间的差异)解决方案是只执行 var rows: Any
然后返回,但这并不是一个好主意。
class CustomObject1{
var number: Int
}
class CustomObject2{
var name : String?
}
我对协议(protocol)的遵守情况:
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
class 没用,它只适用于
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
我该如何解决?
编辑
我认为我的协议(protocol)方法完全没有必要。但是现在我有一个不同的问题。我的 viewController 以前是这样的:
class ViewController: UIViewController{
var dataSource : [Section] = []
}
现在我必须将其更改为:
class ViewController<Row>: UIViewController{
var dataSource : [Section<Row>] = []
}
对吗?
Rich 建议的方法现在的问题是我不能将通用类的这两个实例apppend
到一个数组中,因为它们的通用属性类型最终是不一样的.
最佳答案
我认为您可以通过针对此用例使用通用类来简化解决方案,但如果您想使用协议(protocol)和关联类型,则以下方法应该有效:
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
}
}
如果类不需要实现任何其他功能,这可能会更好
关于ios - 如何编写面向协议(protocol)的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49679511/