如何通过协议枚举数组在Swift中运行?

时间:2019-01-15 13:19:04

标签: swift

我有一个使用协议在另一个类中调用函数的类:

func calculateTableSize () {            
    // doing some stuff  
    // the call to the other function using the protocol
    summaryPresenter?.onCalculateTableSizeDone()     
}

我想使用此函数传递枚举类型的数据数组:

class SummaryInteractor: SummaryScreenInteractorFunctionsProtocol {

    //sections
    enum Section: Int, CaseIterable {
        case header = 0, description, diagnoses, perscription, notes, addFaxHeadline,  addFax, addEmailHeadline, addEmails, givePermissionHeadline, selecAnswer, addNewEmail,addNewFax, removableText, headlineEmpty
    }

    var sectionData: [Section] = [
        .header
    ]

...
...

问题显然是我无法在协议中添加以下行(这是我想要实现的):

//what will be written in the presenterFromTheInteractor
protocol SummaryScreenInteractorProtocol {
    func onCalculateTableSizeDone(data: [Section])
}

因为该协议(以及其他所有类都不知道这个新的Enum类型,即选择是什么)。

因此,它当然会显示错误:

  

使用未声明的类型“ Section”

我如何设法将sectionData传递给其余功能?

谢谢

1 个答案:

答案 0 :(得分:5)

无法从协议访问您的枚举,因为它已嵌入另一个类中。您有两个选择

  1. 将枚举移到外部

    enum Section {}
    
    class SummaryInteractor {}
    
  2. 指定枚举的位置:SummaryInteractor.Section

    func onCalculateTableSizeDone(data: [SummaryInteractor.Section])