简体   繁体   中英

SWIFT: Any Way To Create Nested Enums with Relationships?

I would like to know if its possible to create nested Enums with defined relationships in Swift.

Example:

Cities.Boston.Streets.<streets in Boston>
Cities.Boston.Avenues.<avenues in Boston>
Cities.Chicago.Streets.<streets in Chicago>
Cities.Chicago.Avenues.<avenues in Chicago>
enum Cities {}

extension Cities {
    enum Boston{}
    enum Chicago{}
}

extension Cities.Boston {
    enum Streets {
        case Ave
        case AveN
    }
}

Likewise you can extend for Cities.Chicago then you can access it like Cities.Boston.Streets.Ave

Only reason extensions are added is to have it in a structured way. Its all the same if you define in the main Cities enum as well.

Not sure what you mean when you say you are looking for enums with relationships.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM