Object-C ---) Swift (8) Enumeration

Source: Internet
Author: User

Object-C ---) Swift (8) Enumeration
Declared Enumeration

Swift uses enum to define enumeration. The syntax format is

Enum enumeration name {// use the case keyword to list all enumerated values // other enumerated members}

Swift allows each enumerated value to use one case keyword, and multiple enumerated values can be defined using one case. Multiple enumerated values are separated by commas. From the perspective of program readability, the swift enumeration name must be composed of one or more meaningful words. Each word has an upper-case letter and all other letters have lower-case letters, do not use any delimiter between words.

    enum Weekday{case Mondaycase Tuesdaycase Wednesdaycase Thursdaycase Fridaycase Staturdaycase Sunday}
Call Enumeration
    var day:Weekday   day=Weekday.Sunday

When the program can infer the enumerated type of a variable, Swift allows omitting the prefix of the enumerated name before the enumerated value.

day=.Sunday
Enumeration value and switch statement
Var chooseDay = Weekday. Sundayswitch (chooseDay) {case. Monday, Tuesday, Wednesday, Thursdayv, Friday: print ("workday") case. Staturdayv, Sundayprint ("Weekend ")}
Original Value

You can specify a simple type (such as Int, Double, Float) for each enumerated instance by using the original value)
The syntax format is as follows:

Enum enumeration name: original value type {case enumeration value = original value}

Note that Swift does not require you to specify the original value for each enumerated value. Swift can infer the enumerated values of all the enumerated values before and after the specified enumerated values based on the enumerated values.

enum Weekday:Int{case Monday,Tuesday=1,Wednesday,Thursday,Friday,Saturday,Sunday}

From Tuesday = 1, we can infer that Monday is equal to 0, Wedding is equal to 2, and later is equal to 3, 4...
However, strings cannot be inferred. You must specify the original value for each enumeration.

enum Season:Character{case Spring="S"case Summer="U"case Fall="F"case Winter="W"}
The following constructor and attributes can be used for enumeration:

Init? (RawValue :) constructor that may fail. Therefore, the * constructor returns an optional type that contains enumeration values.

RawValue: attribute (called by enumeration instances ). Obtains the original value of the specified enumerated value.

Var day = Weekday. Staturdayprint (". Saturday's original value is \ (day. rawValue)") // output 8var mySeason = Season (rawValue: "S") if mySeason! = Nil {print (mySeason !) Print ("Spring ")}

Note: It may be a failed constructor, so you need to judge it, otherwise it will cause a crash.
Associated Value
When defining an enumeration, you can add a variety of enumeration attributes for the enumeration, which greatly enriches the enumeration function compared with C and Object-C.

Enum Season {case Monday (Work: String) case Tuesday (Work: String) case Wednesday (Work: String) case Thursday (Work: String) case Friday (Work: String) case Staturday (Work: String) case Sunday (Work: String)} var mySeason1 = Season. monday (Work: "Work ah") var mySeason2 = Season. sunday (Work: "dating ")

To access the associated values of enumerated values, you must declare additional variables or constants and bind the associated values of enumerated values to these variables or constants.

Season.Monday(Work:String)=mySeason1print("\(Work)")

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.