Swift re-identification of enumeration variables

Source: Internet
Author: User

Use enum -to-create an enumeration. Like classes and all other named types, enumerations can has methods associated with them.

The enum in swift changes a lot, and the enumeration looks similar to the class because it can have its own method, and the enum is created as follows

Enum Rank:int {
Case ACE = 1
Case A, 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)
}
}
}

As can be seen in the example above, the type specified at the time of the creation is the int type, which can specify the initial value of the RawValue, and from the beginning of 0, the value of the values in Swift can also be a floating point value and a string.

In the example above, the Raw-value type of the enumeration Int are, so that only has the to specify the first raw value. The rest of the raw values is assigned in order. You can also with strings or floating-point numbers as the raw type of an enumeration. Use the property to rawValue access the raw value of a enumeration member.

As you can see, Swift has a great language flexibility, and using rank (RawValue: < #Int #>) to get a optional that needs to be unpacked before it can be used. The use of enums is relatively simple, with examples and official documents that are easy to grasp. I learn iOS time is shorter, the blog is not the right place to ask you to do a lot of administration.

Swift re-identification of enumeration variables

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.