LoginSignup
1
2

More than 1 year has passed since last update.

【Swift】CaseIterableを使って列挙型の配列を取得する

Posted at

実装

enum WeatherCondition: String, CaseIterable {
    case sunny
    case cloudy
    case rainy
}

print(WeatherCondition.allCases)

// [プロジェクト名.WeatherCondition.sunny, プロジェクト名.WeatherCondition.cloudy, プロジェクト名.WeatherCondition.rainy]

おわり

今回のサンプルは数が少ないのであんまり意味がないように思えますが、TwitterAPIのエラーコードのように大量にある場合は今回の方法が有効です。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2