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

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.

主站蜘蛛池模板: 腾冲县| 上杭县| 平安县| 隆尧县| 大方县| 房山区| 临邑县| 伊宁县| 将乐县| 华亭县| 建瓯市| 禄劝| 康平县| 龙南县| 仁怀市| 南丹县| 罗甸县| 桂平市| 博乐市| 安康市| 青河县| 同心县| 富平县| 曲水县| 长武县| 东兰县| 繁昌县| 化德县| 昌吉市| 眉山市| 武宁县| 盖州市| 固阳县| 沾益县| 扶余县| 城固县| 宜城市| 科技| 开平市| 大连市| 黄冈市|