官术网_书友最值得收藏!

Enumerations

Enumerations are a programming construct that let you define a value type with a finite set of options. Most languages have enumerations (usually abbreviated to enums), including C and, by extension, Objective-C.

An example of an enum from the iOS/macOS SDK is NSComparisonResult, which you would use when sorting items. When comparing for the purposes of sorting, there are only three possible results from a comparison:

  • ascending : The items are ordered in ascending order
  • descending : The items are ordered in descending order
  • same : The items are the same

There are a finite number of possible options for a comparison result; therefore, it's a perfect candidate for being represented by an enum:

enum ComparisonResult : Int { 
case orderedAscending
case orderedSame
case orderedDescending
}

Swift takes the enum concept and elevates it to a first class type. As we will see, this makes enums a very powerful tool for modeling your information.

This recipe will examine how and when to use enums in Swift.

主站蜘蛛池模板: 和顺县| 尤溪县| 太康县| 阳春市| 大兴区| 深水埗区| 泾川县| 云浮市| 临沂市| 收藏| 乐昌市| 汕头市| 耿马| 丁青县| 永城市| 阿拉善左旗| 兰坪| 淳化县| 莱西市| 易门县| 克山县| 扬州市| 铜梁县| 黔西| 建湖县| 九江市| 揭东县| 贞丰县| 泽普县| 苏州市| 海宁市| 民县| 岳阳县| 安仁县| 滁州市| 东城区| 德江县| 正安县| 乐陵市| 平果县| 罗江县|