Swift Learning--enumeration

Source: Internet
Author: User
Tags mul

Swift Enumeration

1. Enumerating the basic syntax

enum Method {

Case Add

Case Sub

Case Mul

Case Div

}

You can also use a simpler notation.

enum method1{

case Add, div,mul,sub

}

2. Swift matches enumeration values

func Choosemethod (Op:method)-(double, double)->double {

Switch op {

//. ADD is actually method.add

case. ADD:

func Add (a:double, b:double)->double{

return A+b

}

return Add

case. Sub:

return {(a:double, b:D ouble), Double in return a-B}

case. Mul:

return {$ * $}

//Case. Div:

return {

(A:D ouble, b:D ouble)->double in

Return A/b

If you do not write the full case value, you must add the default, or the error

default:

return {

$ + $

}

}

}

Let val = Choosemethod (op:. DIV) (10, 131)

Print (val)

Swift Learning--enumeration

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.