Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 嵌套和关联的枚举值_Swift_Enums - Fatal编程技术网

Swift 嵌套和关联的枚举值

Swift 嵌套和关联的枚举值,swift,enums,Swift,Enums,NSHipster在处理表或集合视图时认可的最佳实践之一是使用枚举表示每个部分,如下所示: typedef NS_ENUM(NSInteger, SomeSectionType) { SomeSectionTypeOne = 0, SomeSectionTypeTwo = 1, SomeSectionTypeThree = 2 } 这使得执行switch语句或ifs非常容易,例如: if(indexPath.section == SomeSectionTypeOne)

NSHipster在处理表或集合视图时认可的最佳实践之一是使用枚举表示每个部分,如下所示:

typedef NS_ENUM(NSInteger, SomeSectionType) {
    SomeSectionTypeOne = 0,
    SomeSectionTypeTwo = 1,
    SomeSectionTypeThree = 2
}
这使得执行switch语句或ifs非常容易,例如:

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)
}

但是我不知道如何从
nsindepath
开始,初始化正确的大小写,然后使用switch语句提取值。

不要认为可以使用嵌套枚举来确定节和行单元格的来源。因为关联值和原始值不能在Swift枚举中共存。需要多个枚举

enum sectionType: Int {
    case sectionTypeOne = 0, sectionTypeTwo, sectionTypeThree
}

enum rowTypeInSectionOne: Int {
    case rowTypeOne = 0, rowTypeTwo, rowTypeThree
}

//enum rowTypeInSectionTwo and so on

let indexPath = NSIndexPath(forRow: 0, inSection: 0)

switch (indexPath.section, indexPath.row) {
case (sectionType.sectionTypeOne.rawValue, rowTypeInSectionOne.rowTypeOne.rawValue):
    print("good")
default:
    print("default")
}