swift - 在 Swift 中的协议类型数组上使用 index(of:)
问题描述
为了在我大部分是静态的 tableViews 中增加更多的灵活性,我有一个这样定义的协议:
protocol StaticSection {
static var active: [StaticSection] { get }
// func cell(forRowAt indexPath: IndexPath, tableView: UITableView) -> UITableViewCell
// var numberOfRows: Int { get }
}
extension StaticSection: Equatable {
static func at(_ index: Int) -> StaticSection {
return active[index]
}
static func index(ofSection section: StaticSection) -> Int {
return active.index(of: section) // Not working :(
}
}
我这样使用
enum MySections: StaticSection {
case header, footer, sectionA, sectionB
static var active: [StaticSection] {
// Here I can dynamically enable/disable/rearrange sections
return [MySections.header, .footer]
}
}
在enum
协议的实现中,我可以访问如下部分的索引:
(StaticSections.active as! [MySections]).index(of: .header)
现在我想index(ofSection section: StaticSection)
在扩展中实现,以便有一种更方便的方式来做到这一点。我尝试了如上面扩展中所示的那样。但我得到了错误:
无法使用类型为“(的:StaticSection)”的参数列表调用“索引”
这在 Swift 中是否可行?
解决方案
你可以这样做:
protocol StaticSection {
static var active: [Self] { get } // note the change to Self
// func cell(forRowAt indexPath: IndexPath, tableView: UITableView) -> UITableViewCell
// var numberOfRows: Int { get }
}
extension StaticSection where Self : Equatable { // another change here
static func at(_ index: Int) -> Self {
return active[index]
}
static func index(ofSection section: Self) -> Int? {
return active.index(of: section)
}
}
enum MySections: StaticSection {
case header, footer, sectionA, sectionB
static var active: [MySections] { // note the change to MySections
// Here I can dynamically enable/disable/rearrange sections
return [.header, .footer]
}
}
这里要注意的重要一点是这种语法:
where Self : Equatable
这意味着扩展仅适用于符合StaticSection
and的类型Equatable
,而这:
: Equatable
将使StaticSection
继承自Equatable
,这是您在 Swift 中无法做到的。
推荐阅读
- asp.net - SignalR如何取消绑定方法侦听器
- c# - 在 C# 中的 Model 类中处理 HttpClient 的最佳方法
- node.js - 我怎样才能创建一个可读的管道到一个可写的,这将允许我不时向它添加内容?
- python-3.x - 为背包问题中选择的最小对象数创建约束
- c++ - 我可以为类类型重载 << 运算符以在 C++ 中生成文本和二进制文件,同时能够链接 << 操作吗?
- javascript - 为什么当我复制变量时不起作用
- mongodb - Mongoose updateMany 什么都不做
- vue.js - 在 Vue 中使用 Composition API 将数据传递和分配给状态
- react-native - 向 React Native 项目添加时间选择器
- security - 如何在我的开发环境中绕过 SameSite cookie 限制?