Detailed enumeration types for Swift programming _swift

Source: Internet
Author: User

Presumably the children's shoes that have been programmed are not unfamiliar with enumerated types, the advantages of using enumerated types are many, and there is no more to repeat them here. Enumerations in the Fundation framework and Uikit are numerous, and enumerations can make your code easier to read and improve maintainability. The enumeration in the swift language is a light one. The enumerations in swift retain not only the attributes enumerated in most programming languages, but also some useful and useful new features, and in this article will be a glimpse of the glamour of some of the enumerated types in Swift.

A small partner will ask, is not an enumeration, what to say. How does an enumeration in Swift do otherwise, the functions of the enumeration in Swift are much more powerful, not only to bind values to enumerated elements, but also to associate multiple values with an enumeration element, and to assign an enumeration value to an enumeration variable by its value, and to define an enumeration function in an enumeration. The following is about to begin to pry into the enumerations in Swift.

One, defining enumerations

The enumeration in Swift is different from defining enumerations in other programming languages, with one more keyword case in front of each enumerated element, followed by an enumeration element, and the following are two ways to define an enumeration type.

1. Multiple case, each enumerated element has a case before it

Enumeration definition
enum Kindofanimal {case
 Cat case
 Dog case
 Cow case
 Duck case
 Sheep

2. A case to handle all elements, enumeration elements separated by commas

 You can also define enum type
enum Kindofanimaltwo {case
 Cat, Dog, Cow, Duck, Sheep
 }

Ii. use of enumerated types

The enumeration type is defined for use, right, directly using enumerated types to declare variables, and in swift you do not need a typedef to define enumerated types, we can use enumerated types directly.

Define an enumeration variable and assign a value
var animal1:kindofanimal = Kindofanimal.cat

You can also omit an enumeration type name when assigning a value to an enumeration variable, because the type of the enumeration variable has been specified when the enumeration variable is declared.

var animal2:kindofanimal =. Dog 

Using our enumeration variables in the switch

In switch ... Case using Enumeration
switch Animal1 {case
 kindofanimal.cat:
 println ("Cat") Case
 Kindofanimal.dog:
 println ("Dog") Case
 Kindofanimal.cow:
 println ("Cow") Case
 Kindofanimal.duck:
 println ("Duck") Case
 Kindofanimal.sheep:
 println ("Sheep")
 default:
 println ("error = hehe")
}

Assigning values to enumeration members

When declaring an enumeration in Swift, you can assign a value to each enumeration member, and the members of the city enumeration below are assigned a value, as follows:

assigning enum city:string{case
 Beijing = "Beijing" Case
 Shanghai = "Shanghai" case
 guangzhou = "Guangzhou" Case
 Shengzh En = "Shenzhen"
}

RawValue that use an enumeration variable can get the value assigned to each enumeration member, as follows:

Defines an enumeration variable and assigns
var myincity:city = city.beijing

//Get the value of an enumeration variable
var myincitystring:string = Myincity.rawvalue;
println (myincitystring)//output: Beijing

Assigning values to an enumeration variable by the value of an enumeration member

What is assigning a value to an enumeration variable by the value of an enumeration member? For example, take the above enumerated type city as an example, if we only know that the value of an enumerated member is "Beijing", and that the enumeration member of "Beijing" is not known to be "Beijing", in swift it is possible to assign an enumeration member "Beijing" to an enumeration variable through the value "Beijing". Of

Isn't it a bit of a detour, a bit of an example, and here's the code that assigns values to an enumeration variable by the original value of the enumeration member

Assign a value to an enumeration member by enumerating the values of members
var youincity:city = City (rawValue: "Beijing");

Why is our youincity an optional value type? The reason is very simple, we are not sure whether the value of the member in the enumeration city contains "Beijing", the value of the enumeration variable youincity is indeterminate, so it is an optional type, the following we take out the youincity value, first judge whether the youincity is city.beijing , and if so, the value is output if the assignment succeeds.

Remove the value from youincity
if youincity = = city.beijing {
 var cityname:nsstring = youincity!. RawValue
 println (cityname)//output: Beijing
}

Find a string that is not contained in the value of an enumeration member to assign a value to an enumeration variable, and to observe the result, the value of the following testcity is printed as nil because no member of the enumeration has the value "Beijing".

Pass in a value that is not in an enumeration
var testcity:city? = City (rawValue: "Beijing");

Testcity for nil
println (testcity)

V. Enumeration value self-increasing

The good things are still to be preserved, and if the enumerated values in Swift are integers, the first one is assigned, and the back will increase. About the value of the enumeration is not much to say, directly to see the example bar.

Enum value self-add
enum hour:int{case One
 = 1 case two case Three case Four case
 Five case
 Six Case
 Seven case
 eight
}

var hourtest:int = Hour.Eight.rawValue
println (hourtest)// Hourtest = 8

Vi. Enumeration Association values

What is the associated value of an enumeration? The literal meaning is to associate a value with an enumeration member, and yes, to associate a value with an enumeration variable when assigning a value to an enumeration variable. How do you do it in Swift? It is when you declare an element in an enumeration type that you use parentheses to create the type of the associated value, and then when you assign a value to an enumeration variable, you can associate one or more values, and see the instance directly.

The following code specifies two string-type association values for iOS, associating two values when assigning values to an enumeration variable. The associated values can be used in a switch statement.

enumerated associated values
enum mobilelanguage{case
 IOS (String, string) case
 Android (String)
}

var iPhone: Mobilelanguage = Mobilelanguage.ios ("Objective-c", "Swift")

switch IPhone {case
 Mobilelanguage.ios Language1, let Language2):
 println ("language1 = \ (language1), language2 = \ (language2)")
 
 case Mobilelanguage.android (Let temp):
 println (temp);
 Default:
 println ("NO")
}

//output result: Language1 = objective-c, Language2 = Swift

Vii. enumeration functions

Enumerations in Swift can add functions, and there is no light in front of them. The following code snippet adds a descriptive function based on the associated value code above, and returns the enumeration information for the current enumeration variable, as shown in the following code snippet:

enum function
enum mobilelanguagefun{case
 IOS (String, string) case
 Android (String)
 //define enum function
 var description:string{
 Switch Self {case
 Mobilelanguagefun.ios (Let language1, let Language2): Return
 " Language1 = \ (language1), language2 = \ (language2) "Case
 
 mobilelanguagefun.android [Let temp]: return
 Temp
 Default: Return
 ("NO")
 }

 }

var mymobile:mobilelanguagefun = Mobilelanguagefun.ios ("OBJC", "Swift")

println (mymobile.description)//language1 = OBJC, Language2 = Swift

This is the full content of this article, I hope to learn more about Swift software programming help.

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.