How to iterate over all enum values with String type in Swift?
Hello everyone! I just started learning Swift and encountered a problem when working with enumerations (enum). I have an enum that contains string values for product categories:
```swift
enum Category: String {
case electronics = "Электроника"
case clothing = "Одежда"
case books = "Книги"
case sports = "Спорт"
}
```
I need to display all available categories as a list for a picker, but I can't figure out how to iterate over all cases of this enum. I tried using `Category.allCases`, but the compiler complains that there is no such property. I also tried `for category in Category`, but it doesn't work either.
The documentation mentions the `CaseIterable` protocol, but I don't fully understand how to apply it correctly. When I add `CaseIterable`, the code compiles
No answers yet.
Log in to leave an answer
Blogs
Error Handling in Kotlin: try/catch and Advanced Techniques