NSHipster推荐处理表格或集合视图时使用枚举来表示每个部分的最佳实践之一,示例如下:
在Swift中,我想要复制相同的行为,但这次利用嵌套枚举。到目前为止,我已经想出了以下内容:
typedef NS_ENUM(NSInteger, SomeSectionType) {
SomeSectionTypeOne = 0,
SomeSectionTypeTwo = 1,
SomeSectionTypeThree = 2
}
这使得编写类似于switch语句或if语句非常容易,例如:
if(indexPath.section == SomeSectionTypeOne) {
//do something cool
}
对于静态内容的部分,我将概念扩展到每个项目都包括枚举:
typedef NS_ENUM(NSInteger, SectionOneItemType) {
ItemTypeOne = 0,
ItemTypeTwo = 1
}
if(indexPath.section == SomeSectionTypeOne) {
switch(indexPath.item) {
case SectionOneItemType:
//do something equally cool
default:
}
}
在Swift中,我想要复制相同的行为,但这次利用嵌套枚举。到目前为止,我已经想出了以下内容:
enum PageNumber {
enum PageOne: Int {
case Help, About, Payment
}
enum PageTwo: Int {
case Age, Status, Job
}
enum PageThree: Int {
case Information
}
case One(PageOne)
case Two(PageTwo)
case Three(PageThree)
}
但我不知道如何从一个 NSIndexPath
开始初始化正确的情况,然后使用 switch 语句提取值。