Beginner Swift Notes Enumeration (vii)

Source: Internet
Author: User
Tags mul

1 Import Foundation2 3 /*4 Enumeration5 6 Grammatical Structure7 enum type {8 Case Value9 }Ten */ One //define an enumeration notation 1 A enumLiushiqun { -      CaseName -      Case Age the      CaseSex -      CaseADD -      CaseSub -      CaseMul +      CaseDiv - } + //define an enumeration notation 2 A enumliushiqun_1 { at      Caseadd,sub -      CaseMul -      CaseDiv - } - //define an enumeration variable notation 1 -Let Enum_name:liushiqun = . Name in //define an enumeration variable notation 2 -Let Enum_age:liushiqun =Liushiqun.age to //defining enumeration variables notation 3 +Let Enum_add =Liushiqun_1.add -  the //combine the various ways of closures into methods with switch * //when matching, if the enumeration does not have a case for all values, switch will error or add the default at the end $Func Choosemethod (op:liushiqun_1), (double,double)Double {Panax Notoginseng     SwitchOP { -      Case . ADD: theFunc Add (a:double,b:double),Double { +                 returnA +b A         } the             returnAdd +      Case . Sub: -             return{(a:double,b:double)-Doubleinch returnAB} $      Case . Mul: $             return{return$0* $1} -      Case . Div: -             return{$0/ $1} the     default : -         return{$0+ $1}Wuyi     } the } - //declares an object that invokes a method that passes an enumeration value that is worth to a closure type WuLet Val_1 =Choosemethod (. Mul) - //calling Val_1 closures AboutLet value_2 = Val_1 (4,7) $ println (value_2) -  - /* - enumerating the original value definitions A enum Lsq:int { + Case Add,sub,mul,div the } - If you do not specify a value, start at 0, and then click +1 $ Lsq.Add.toRaw () gets the original value of the Lsq.add-corresponding Fromraw pushes the value back to the enumeration value the Lsq.Add.rawValue Get the original value of Lsq.add the  the  the enum Lsq_1:int { - Case add=0,sub=1,mul=3,div=4 in } the  the enum Lsq_2:string { About Case add= "Add", sub= "Sub", mul= "Mul", div= "Div " the } the */ the  + enumLsq_1:int { -      CaseAdd=0, Sub,mul,div the }Bayi println (Lsq_1.Add.rawValue) the enumlsq_2:string { the      CaseAdd="Add", sub="Sub", mul="Mul", div="Div" - } - println (Lsq_2.Add.rawValue) the  theLet op:lsq_2? = Lsq_2 (rawValue:"Add") the println (OP) the //string Goto enumeration value - func get_enums (str:string) { theLet new_op:lsq_2? =lsq_2 (RAWVALUE:STR) the     if varTemp_op =New_op { the         SwitchTemp_op {94          Case . ADD: theprintln"ADD") the          Case . Div: theprintln"Div")98          Case . Mul: Aboutprintln"Mul") -          Case . Sub:101println"Sub")102         default :103println"error: \ (str)")104         } the     }106     Else {107println"No")108     }109 } theGet_enums ("adds")111  the //Enumerate related values113 //Two Value Association enumeration value the enumLinesegmentdescriptor { the      CaseStartandendpattern (start:double,end:double) the      CaseStartandlengpattern (start:double,length:double)117 }118 //defines two associated values for a variable that specifies an enumeration value119 varLSD = Linesegmentdescriptor.startandendpattern (start:1, End:Ten) -LSD = Linesegmentdescriptor.startandlengpattern (start:1, Length:9)121 //Extract the Values122 SwitchLSD {123 //Case let . Startandendpattern (S, e)://switch Value binding mode124  Case. Startandendpattern (Let S,let e)://switch Value binding mode theprintln"start \ (s) end \ (e)")126  Case. Startandlengpattern (Let S,let e)://switch Value binding mode127println"start \ (s) end \ (e)") -}

Beginner Swift Notes Enumeration (vii)

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.