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

How to do it...

Earlier, we created a Person object to represent people in our model and a PersonName struct to hold information about a person's name. Now, let's turn our attention to a person's title, for example, Mr., Mrs., and so on, which precede someone's full name. There are a small and finite number of common titles that a person may have; therefore, an enum is a great way to model this information.

Enter the following into the playground:

enum Title { 
case mr
case mrs
case mister
case miss
case dr
case prof
case other
}

We define our enumeration with the enum keyword and provide a name for the enum. As with classes and structs, the convention is that this starts with a capital letter, and the implementation is defined within curly brackets. We define each enum option with the case keyword, and the convention since Swift 3 is that these start with a lowercase character. Now that we have defined our enum, let's see how to assign it:

let title1 = Title.mr 

Enums can be assigned by specifying the enum type, then a dot, and then the case. However, if the compiler can infer the enum type, we can omit the type and just provide the case, preceded by a dot:

let title2: Title 
title2 = .mr
主站蜘蛛池模板: 施秉县| 营口市| 河源市| 仁怀市| 茌平县| 湖州市| 霍林郭勒市| 辽宁省| 盈江县| 九江县| 绥德县| 恩施市| 屏东县| 台前县| 紫金县| 铜梁县| 土默特右旗| 广州市| 西充县| 合作市| 长治市| 文安县| 临清市| 龙游县| 将乐县| 资溪县| 达州市| 九江市| 六安市| 临颍县| 呼伦贝尔市| 武宁县| 卢湾区| 抚州市| 崇文区| 青铜峡市| 吉木乃县| 玉门市| 聂荣县| 连州市| 昌乐县|