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

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.

主站蜘蛛池模板: 黑龙江省| 靖西县| 黔西县| 宁波市| 驻马店市| 泗水县| 韶关市| 宜章县| 大洼县| 乌审旗| 阜城县| 马尔康县| 吴江市| 鄂托克前旗| 二连浩特市| 泊头市| 仪陇县| 神木县| 常州市| 夹江县| 乐都县| 临海市| 安多县| 金华市| 嘉鱼县| 盐亭县| 岐山县| 延寿县| 望奎县| 贡觉县| 府谷县| 库尔勒市| 甘南县| 手游| 翁牛特旗| 勃利县| 霞浦县| 高淳县| 唐山市| 海兴县| 宝鸡市|