Swift Syntax 01-enum type

Source: Internet
Author: User

Read Source: Official Swift2.0 Tutorial Chinese version

Today we learned the enumeration types of swift, and the following are the contents and summaries:

Use enum to create an enumeration. Like classes, enumerations can also contain methods.

enum rank: int {   case ace =1   case two,  three, four, five, six, seven, eight, nine, ten   case  Jack, queen, king       func simpledescription ()  ->  String {      switch self {          case . ace: return  "Ace"          CASE . jack: return  "Jack"          CASE  queen: return  "Queen"          CASE . king: return  "King"          default: return  String (Self.rawvalue)                   }          }}let ace = rank.aceletacerawvalue =  Ace.rawvalue

Practice:

Write a function that compares two Rank instances with the original values.

In the example above, the type of the enum original value is Int, so you only have to set the original value of the first case. The remaining raw values are assigned in order. You can also use a string or a floating-point number as the original value of the enumeration. Use the RawValue property to access the original value of an enumeration member.

Use init? (RawValue:) The constructor method transforms between the original value and the enumeration.

If Let Convertedrank = Rank (rawvalue:3) {Let threedescription = Convertedrank.simpledescription ()}

If you do not use the original value, you can not set it.

Enum Suit {case spades, Hearts, diamonds,clubs func simpledescription ()-String {switch Self { Case. Spades:return "Spades" case. Hearts:return "Hearts" case. Diamonds:return "Diamonds" case. Clubs:return "Clubs"}}}let hearts = Suit.heartslet heartsdescription = hearts.simpledescription ()

Note that there are two ways to refer to a Hearts member: When assigning a value to a Hearts constant, because it does not explicitly specify a type, Suit.hearts is represented by its full name. In switch, because the type of self is known, enumeration members can use abbreviations such as. Hearts to reference.

Swift Syntax 01-enum type

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.