Enumerations define a set of related values for a common type, so that you can use them in a safe way in your code.
Defines the syntax of an enumeration enum Chips { //Defines the enumeration of potato chips, containing 3 flavors of members case Tomato case Barbecue}
Each enumeration defines a completely new type. Like other types in Swift, their names must begin with a capital letter. A single-digit name instead of a plural name for an enumeration type to make it easier to read.
The generic Match enumeration value is used by the switch
var chip = chips.original//matches the enumeration value of switch chip {case. Tomato: print ("Tomato") case. Original: print ("Original") case. Barbecue: print ("Barbecue")}
The above example shows the basic syntax of an enumeration, and the following is a record of the value assigned to a member value for any property, which can be stored together in some special cases.
< if > customizes a control that requires the display of a control (such as the button's custom) enum ViewType {case Normal (String) //For example, a string to be displayed Case Contains (double, double) //For example, need to pass in the display scale}var ViewType = Viewtype.normal ("passed the string that needs to be displayed (given the relevant value)") Switch ViewType {case. Normal (Var str): print ("Sets the string the control needs to display"); Contains (Widthscale, Hightscale): print ("Set the control's wide-height scale");
As an alternative to a related value, an enumeration member can be assigned a value by default (called the original value), where the original values have the same type.
Enum Month:int { //Because the original value type must be consistent, define the original value as Int Note: The original value and the correlation value cannot be used in both case January//If the first member is implicitly assigned a value of 0 without a given original value , each subsequent member increments 1 case February case March case April}var February = Month.februaryprint (February, February.rawvalue) //output: "February 1\n"
Enum Month:int { //can also reduce all member values to 1 rows with the same effect as before, case January = 1, February, March, April}var February = Month.febr Uaryprint (February, February.rawvalue) //output: "February 2\n"
Note: If the initial value is float or double, the default increment is 1
Enum Month:string { //If it is the initial value of a string type and does not have an explicit assignment, set the member name to the initial value case January = "January", February, March, April}var Jan uary = Month.januaryprint (January, January.rawvalue) //output: "January January \ n" var March = Month.marchprint (March, March.rawvalue) //output: "March march\n"
Let's take a look at the original values to construct the enumeration
Enum Viewcontenttype:int { //simulates a view's content mode case Normal = 0, System, Custom, ancient}//constructs an enumeration if let type = V based on the original value Iewcontenttype (rawvalue:2) { //Note the original value constructor is a failed constructor (which may return nil) so the optional type is returned, and the optional binding switch type {case is required . Normal: //do something ... Print (type) case . System: //do something ... Print (type) default: //Do sonthing ... Print (type) //Last output of this sentence }}
The following is a comparison of the new features of the enumerated swift enumeration in OC: The enumeration in Swift can define type attributes and instance properties
< if > customizes a control that needs to be displayed as a control (such as custom for button) enum ViewType { //We take this enumeration example if declaring an instance property can return a size that corresponds to the member value Viewsize is a read-only computed property var viewsize:cgsize { switch self { ///parameter is temporarily omitted //can be returned according to different members corresponding to the size case . Normal (_): return Cgsize (width:100, height:20) case . Contains (_, _): return Cgsize (width:390, height:300) } } //Declaring type property static Let comment = "This enumeration has two A member, setting different values to change the control's Mercy State "case Normal (String) ///need to pass in a string to be displayed case Contains (double, double) // For example, you need to pass in the display scale}let type = Viewtype.normal ("a string")//Call Instance Calculation Property Get Sizelet viewsize = type.viewsize//Call Type Calculate property print ( Viewtype.comment)
Enumerations can also declare methods
Enum Month:int { //Because the original value type must be consistent, define the original value as Int Note: The original value and the correlation value cannot be used together //enumeration You can declare a method just like a class, but if you modify your value, you need to add a keyword. mutating func nextmonth (), String { switch self {case January: ' Self = February return ' has been pulled by February ' Case . February: Self = March return "March pull" default:return "etc" }} case January // If no original value is given, the first member is implicitly assigned a value of 0, and each subsequent member is incremented by 1 case, February , March, April}var month = month (rawvalue:0) c19/>//Januarylet string = month?. Nextmonth () //Returns a string and changes the member value to Februaryprint (month) //Print out "Optional (month.february) \ n"
A preliminary understanding of learning swift--enumeration--Personal memo big God don't squirt