我有以下代码:
class SetupVC: UIViewController {
var currentStep:SetupStep?
var currentStepIndex:Int = 0
enum SetupStep {
case basic(BasicStep), advanced(AdvancedStep)
enum BasicStep: CaseIterable, Equatable {
case one, two, three
}
enum AdvancedStep: CaseIterable, Equatable {
case four, five, six
}
}
override func viewDidLoad() {
super.viewDidLoad()
currentStep = SetupStep.BasicStep.allCases[currentStepIndex]
}
}
它会产生以下错误:
Cannot assign value of type 'SetupVC.SetupStep.BasicStep' to type 'SetupVC.SetupStep?'
我在这里缺乏对嵌套枚举的某种基本理解。必须能够将“BasicStep”存储在“SetupStep”变量中。我错过了什么?
谢谢!
在我看来,您正在尝试让所有设置步骤可用并稍后对其进行迭代。
如果是这样,您还需要为根定义
allCases
,例如:
enum SetupStep: CaseIterable {
static var allCases: [SetupStep] {
BasicStep.allCases.map { .basic($0) } +
AdvancedStep.allCases.map { .advanced($0) }
}
,,,
}
然后在你的VC中:
currentStep = .allCases[currentStepIndex]