Enumerations Enumeration
Use an enum to create an enumeration. As with classes (class) and other types of naming methods, enumerations can also have method (methods).
Enum Rank:int {case
aces = 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 to
"Jack" case
. Queen: Return to
"Queen" Case
. King: Return
"King"
default: Return
String (Self.toraw ())}}} let
ace = Rank.ace let
acerawvalue = Ace.toraw ()
Practice:
Create a function that compares the values of two rank by the analogy of the original values.
In the example above, the type of the original value is Int, so you can just specify the first original value, since the original values are assigned in order. You can also use a string or floating-point number as the original value of the enumeration.
You can use the Toraw and Fromraw functions to implement conversions between the original values and the enumerated values:
1 if let Convertedrank = Rank.fromraw (3) {
2 Let threedescription = Convertedrank.simpledescription ()
3}
The enumerated value is the actual value, not the original value written in other ways. (The meaning of this sentence should be that the enumeration value and the original value are not necessarily related) in order to prevent enumeration of meaningless original values, you do not need to intentionally provide a raw value:
Enum Suit {case
spades, Hearts, diamonds, clubs func
() simpledescription String {
switch self {
cas E. Spades: Return to
"spades" case
. Hearts: Return to
"Hearts" case
. Diamonds: Return to
"Diamonds" case
. Clubs: Return "Clubs"}}} let
hearts = suit.hearts let
heartsdescription = Hearts.simpledescription ()
Practice:
Create a method named color for the enumeration suit, let spades and clubs return "black", let hearts and Diamonds return "red".
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/
Note the two ways in the example that refer to Hearts members: when assigning a value to a constant hearts, Suit.hearts is the full name reference, because the constant hearts at this time does not have an explicit type. And within the switch, the enumeration is done in abbreviated form:. Hearts, because the value of self is known to the enumeration member. When the type of the value is known, you can always use the abbreviated form (to refer to it).
Structures structure
Use the struct keyword creation to create a structure. Many of the behavior of the struct support class (Classes) are: For example, methods (methods) and constructors (initializers). The most important difference between a struct and a class is that, in code, a struct implements the transfer of values by copy (copy), whereas a class is by reference (reference):
struct Card {
var rank:rank
var suit:suit
func simpledescription ()-> String {return
"" (Rank.sim Pledescription ()) of \ (Suit.simpledescription ()) "
}
} let
threeofspades = Card (rank:. Three, suit:. Spades) Let
threeofspadesdescription = Threeofspades.simpledescription ()
Practice:
Add a card method to create a deck of cards with each card containing a rank and suit combination.
An instance of an enumeration member can have the value of an instance. Instances of the same enumeration member can have different values. You can specify a value for the instance when you create it. The difference between the specified value and the original value is that the original value of the enumeration is the same as for all instances, and the original value is provided when you define the enumeration.
For example: There is a scene that requires you to request the sun to rise and fall from the server, the server can respond to give you the appropriate information, but also to return the wrong information:
Enum Serverresponse {case result
(string, string) case
Error (string)
} let
success = Serverresponse.result ("6:00 AM", "8:09 pm") let
failure = Serverresponse.error ("Out of cheese.")
Switch Success {case let
. Result (Sunrise, Sunset): let
serverresponse = "Sunrise are at \ (Sunrise) and sunset are at \ (sunset)."
Case Let. Error (Error)://Request returned information--joe.huang let
serverresponse = "Failure ... \ (Error) "
}
Practice:
Add a third condition (case) to Serverresponse in the switch statement.
Please note: (in the above example) the sunrise and sunset time returned by Serverresponse is the match in the switch (case).
Author: cnblogs Joe.huang