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

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.

主站蜘蛛池模板: 渭南市| 绥中县| 阿拉善右旗| 无为县| 昌平区| 安顺市| 舞阳县| 庆阳市| 昆山市| 石狮市| 册亨县| 视频| 淅川县| 加查县| 罗江县| 怀安县| 常宁市| 墨竹工卡县| 年辖:市辖区| 福安市| 平和县| 蕲春县| 沁水县| 修武县| 泰安市| 东兰县| 郧西县| 余干县| 容城县| 洛阳市| 永嘉县| 大理市| 新津县| 巧家县| 康马县| 郸城县| 大石桥市| 马龙县| 平舆县| 汾阳市| 萨迦县|