Swift-class, struct, enum

Source: Internet
Author: User

001-Enumeration

//enumeration is not assigned to the default integer value in Swiftenumsomeenumeration { Case North CaseSourth CaseEast CaseWest}//multiple member values to be written in one line, separated by commasenumPlanet { CaseMercury,earth,mars}var Directionhead=someenumeration.west Directionhead=. East//using switch to match a single enumeration value        SwitchDirectionhead { Case. North:print (" North")         Case. Sourth:print ("Sourth")         Case. West:print ("West")         Case. East:print ("East")        default: Print ("None of this Direction")        }

Enumerate associated values

 enum   BarCode { case  UPC (int,int,int,int) //  According to the digital identification commodity  case  qrcode (String) // 
    
    }  
    //  
     Create a barcode  var productbar = BARCODE.UPC (
    8 , 
    8 , 
    3 , 
    4  
    ) Productbar  =. QRCode (
     " 
    abcde  
    " ) 
    // 
    UPC replaced by QRCode  
   

Enumerate original values

//the original value of the enumeration hermit assignmentenumAsciicontrolchar:character { Casetab ="\ t"     Caselinefeed ="\ n"     CaseCarriagereturn ="\ r"}//the default value of the string is the enumeration value itselfenumplanet:int{ CaseMercury =1, Venus,earth,mars}//gets the original value of the enumeration variableLet Earthorder =Planet.earth.rawValue Print (earthorder)//output is 3

Enumerate recursion

//Enumerate recursionIndirectenumArithmetic { CaseNumber (Int) Caseaddition (arithmetic,arithmetic) Casemultiplication (arithmetic,arithmetic)}//Create an enumeration recursive expressionLet five = Arithmetic.number (5) let four= Arithmetic.number (4) Let sum=Arithmetic.addition (five, four) let product=arithmetic.multiplication (sum, five)//(5+4) * 5Print (product)

Class

// declaration of the class: class student{    ""    // access Properties   "  Kid "   print (classitem.name)// Kid

//Structure :

// Structural Body struct Teacher {    ""    = teacher () print (myteacher)/ /Teacher (name: "", age:20)

Swift-class, struct, enum

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.