简体   繁体   English

如何快速从枚举成员原始值制作数组字符串

[英]How to make an array string from enum member raw value in swift

I am new in programming and swift. 我是编程新手,很快。 I have an enum like this 我有一个像这样的枚举

enum City : String {
    case tokyo = "tokyo"
    case london = "london"
    case newYork = "new york"
}

Could I possibly get that city name to an array from enum raw value? 我能否从枚举原始值中将该城市名称转换为数组? I hope I can get something like this : 我希望我能得到这样的东西:

let city = ["tokyo","london","new york"]

Something like this. 这样的事情。

let cities = [City.tokyo, .london, .newYork]
let names = cities.map { $0.rawValue }
print(names) // ["tokyo", "london", "new york"]

To get all enum values as an array see this . 要获得所有枚举值作为数组, 请参见this

Swift 4.0 迅捷4.0

If you want to iterate through enum you can do like this. 如果要遍历枚举,可以这样做。

enum City : String {

    case tokyo = "tokyo"
    case london = "london"
    case newYork = "new york"

    static let allValues = [tokyo,london,newYork] 
}

let values = City.allValues.map { $0.rawValue }
print(values) //tokyo london new york

Hope this might help. 希望这会有所帮助。 Please look into this https://stackoverflow.com/a/28341290/2741603 for more detail 请查看此https://stackoverflow.com/a/28341290/2741603了解更多详细信息

enum City : String {
    case tokyo = "tokyo"
    case london = "london"
    case newYork = "new york"
}

func iterateEnum<T: Hashable>(_: T.Type) -> AnyIterator<T> {
    var k = 0
    return AnyIterator {
        let next = withUnsafeBytes(of: &k) { $0.load(as: T.self) }
        if next.hashValue != k { return nil }
        k += 1
        return next
    }
}


var cityList:[String] = []
for item in iterateEnum(City.self){
    cityList.append(item.rawValue)

}
print(cityList)

Using Rintaro answer as inspiration I have created two protocols to be used in simple Enumerations. 以Rintaro的答案为灵感,我创建了两个可在简单枚举中使用的协议。 One to extract all cases and another to extract all rawValues: 一种提取所有个案,另一种提取所有rawValue:

protocol Enumeratable: Hashable {
    static var cases: [Self] { get }
}
extension Enumeratable {
    static var cases: [Self] {
        var cases: [Self] = []
        var index = 0
        for element: Self in AnyIterator({
            let item = withUnsafeBytes(of: &index) { $0.load(as: Self.self) }
            guard item.hashValue == index else { return nil }
            index += 1
            return item
        }) {
            cases.append(element)
        }
        return cases
    }
}

protocol RawValued: Hashable, RawRepresentable {
    static var rawValues: [RawValue] { get }
}
extension RawValued {
    static var rawValues: [RawValue] {
        var rawValues: [RawValue] = []
        var index = 0
        for element: Self in AnyIterator({
            let item = withUnsafeBytes(of: &index) { $0.load(as: Self.self) }
            guard item.hashValue == index else { return nil }
            index += 1
            return item
        }) {
            rawValues.append(element.rawValue)
        }
        return rawValues
    }
}

Playground testing: 游乐场测试:

enum City: String, RawValued, Enumeratable {
    case tokyo, london, newYork = "new york"
}

City.cases       // [tokyo, london, newYork]
City.rawValues   // ["tokyo", "london", "new york"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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