swift 遍历enum

func iterateEnum<T: Hashable>(_: T.Type) -> AnyGenerator<T> {
    var i = 0
    return anyGenerator {
        let next = withUnsafePointer(&i) { UnsafePointer<T>($0).memory }
        return next.hashValue == i++ ? next : nil
    }
}

enum Enum {
    case 🍉
    case 🍊
    case 🍌
}


for f in iterateEnum(Enum) {
    print(f)
}
坚持原创技术分享,您的支持将鼓励我继续创作!