- enum に CaseIterable を継承させる。
enum.playground
import Foundation
enum Numbers: String, CaseIterable {
case one = "1st"
case two = "2nd"
case three = "3rd"
case four = "4th"
case five = "5th"
static func get(name: String) -> Numbers? {
for list in Numbers.allCases {
if list.rawValue == name {
return list
}
}
return nil
}
}
Numbers.get(name: Numbers.two.rawValue) // two
Numbers.allCases // [one, two, three, four, five]
for list in Numbers.allCases {
print(list.rawValue)
// 1st
// 2nd
// 3rd
// 4th
// 5th
}