在Swift中对协议类型的数组使用索引(of:)

在Swift中对协议类型的数组使用索引(of:),swift,generics,swift-protocols,Swift,Generics,Swift Protocols,为了在我的静态表视图中增加更多的灵活性,我定义了如下协议: protocol StaticSection { static var active: [StaticSection] { get } // func cell(forRowAt indexPath: IndexPath, tableView: UITableView) -> UITableViewCell // var numberOfRows: Int { get } } extension Stat

为了在我的静态表视图中增加更多的灵活性,我定义了如下协议:

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 :(
    }
}
(StaticSections.active as! [MySections]).index(of: .header)
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]
    }
}
我是这样用的

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
实现中,我可以访问如下节的索引:

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 :(
    }
}
(StaticSections.active as! [MySections]).index(of: .header)
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]
    }
}
现在我想在扩展中实现
索引(ofSection-section:StaticSection)
,以获得更方便的方法。 我试过了,就像上面分机中显示的那样。但我得到了一个错误:

无法使用类型为“(of:StaticSection)”的参数列表调用“索引”


这在Swift中可能吗?

您可以这样做:

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 :(
    }
}
(StaticSections.active as! [MySections]).index(of: .header)
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
equalable
的类型,而:

: Equatable

将使
StaticSection
继承自
equalable
,这在Swift中是无法做到的。

您的参数不应该是
MySections
类型吗?因此,也许您应该尝试将
StaticSection
的值强制转换为
MySection
:equalable将使StaticSection从equalable继承,这在Swift中是无法做到的。
StaticSection
equalable
都是协议。难道它不只是将
equalable
的要求添加到
StaticSection
,还是我遗漏了什么?目的我的意思是,你不能让一个协议在扩展中继承另一个协议。。并且使其具有条件一致性并不会对主协议产生额外的限制,因为它只在Self是相等的情况下添加扩展。聪明!回答得很好,谢谢!我不知道像这样在协议中使用
Self
是允许的:)