LoginSignup
0
2

More than 3 years have passed since last update.

enum は MECE である

Last updated at Posted at 2019-07-14

MECE_enum.gif
先日
enum はなぜ便利なのか
というタイトルで投稿しました。
今回は MECE な性質がある enum について書きます。

MECE とは?

Mutually Exclusive and Collectively Exhaustive の頭字語でミーシーと読みます。
「重複がなく漏れもない」ことを表します。5択のマークシート試験の答案みたいなもので、5つの選択肢の中に必ず1つだけ正解があるようなものです。
日・月・火・水・木・金・土 などの曜日も MECE な性質があり、どの日も必ずどれか1つの曜日に当てはまります。

Xcode のパーツでは UISegmentedControl も必ずどれか1つの項目が選択されているので MECE な性質であると言えます。

enum で MECE なものの項目を表現する

MECE の「複数の選択肢の中から必ずどれか1つが選択される」という性質を表すのにもってこいなのが enum です。

補足: デフォルトの rawValue

swift では rawValue の記載を省略した場合、以下のようになります。

enum Signal: Int {
        case blue
        case red
        case yellow
    }

とすると、それぞれの rawValue は 0, 1, 2 となります。

enum Signal: String {
        case blue
        case red
        case yellow
    }

とすると、それぞれの rawValue は "blue", "red", "yellow" となります。

0
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
0
2