Swift Basic syntax-enumeration, enumeration of original values, enumeration of related values, switch Fetch enumeration association values

Source: Internet
Author: User
Tags mul
1. Swift enumerates basic conceptsThe enumeration in Swift is more powerful than the enumeration in OC, because the enumeration in Swift is a first-class type.

It can add properties and methods like classes and structs

format:

Enum method{Case    
    enumeration value
}
Enum method{case
    Add Case Sub Case Mul case
    Div
}
2. Basic use of Enumerations You can enumerate values that can be written in a row, but separated by commas
Enum MENTHOD1 {case
    Add, Sub, Mul, Div
}
You can use an enumeration type variable or a constant to receive an enumeration value
var M:method =. Add
Note: If a variable or constant does not specify a type, then you must precede which enum type the value belongs to
var m1 =  . ADD//Direct error
var m1 = method.add//correct spelling
using switch matching
Note: If the case contains all the values, you can not write default. If the case does not contain all of the values in the enumeration, you must write the default
Note: If the case contains all the values, you can not write default.
If the case does not contain all of the values in the enumeration, you must write the default
switch (method.add) {cases
Method.add:
    print ("add") cases
method.sub :
    print ("subtraction") Case
Method.mul:
    Print ("division") Case
Method.div:
    print ("multiply")
default:
    Print ("None")
}
3. Original ValueThe essence of an enumeration in OC is an integer, so the enumeration in OC has the original value, and the default is to start with 0 and the enumeration in Swift has no original value by default, but you can tell the system at the time of the definition that the enumeration has the original value and the enumeration in OC, or you can specify the original value, followed by the default +1, But you must specify the type of the enumeration, and must be an INT type an enumeration in Swift can specify other types in addition to shaping, but if you specify a different type, you must assign values to all enumerated values because you cannot automatically increment
Specifies that the enumeration's original value type is Int, and therefore the first enumeration value is defined with the original value, the following value defaults to +1
enum method3:int{case
    Add = 5, Sub, Mul, Div
}
The type of the custom enumeration's original value is not an Int, so the following value will not default to +1, 
//The following will also need to be manually assigned
enum method4:double{case
    Add = 5.0, Sub = 6.0, Mul = 6.1, Div = 8.0
}
the original value is converted to an enumeration value
Enum method5:string{case    
    Add = "Add", Sub = "Sub", Mul = "Mul", div = "div"
}
get enum Raw value
The RawValue represents the conversion of an enumeration value to the original value, noting that the method converted to the original value in the old version is named Toraw
Method4.Sub.rawValue
Print (Method4.Sub.rawValue)
//Output results: 6.0
create an enumeration value from the original value
The original value case-sensitivity returns an optional value, because the enumeration value corresponding to the original value does not necessarily exist in the old version of Fromraw ("add")
Let m2 = Method5 (rawValue: "Add")
print (m2)

func choosemethod (op:string) {
    //Because the return is an optional type, it is possible to nil, It is best to use the optional binding
    if let OpE = Method5 (rawvalue:op) {
        switch (OpE) {case
        . ADD:
            Print ("addition") case
        . Sub:
            print ("subtraction") case
        . Mul:
            Print ("division") Case
        . Div:
            print (multiplication)}}
        }
4. Enumeration Correlation ValuesThe original value that corresponds to the enumeration value is not unique, but rather a variable. Each enumeration can be some specific value in a pattern
Enum linesegmentdescriptor{case
    Startandendpattern (start:double, end:double) case
    Startandlengthpattern ( Start:double, length:double)
}

var LSD = Linesegmentdescriptor.startandlengthpattern (start:0.0, Length: 100.0)
LSD = Linesegmentdescriptor.startandendpattern (start:0.0, end:50.0)

print (LSD)
//Output Result: Startandendpattern (0.0, 50.0)
Extracting enumeration association values with switch
Switch LSD {case let
. Startandendpattern (S, e):
    print ("start = \ (s) end = \ (e)") Case
. Startandlengthpattern (let-S, let L):
    print ("start = \ (s) lenght = \ (l)")
}
//output result: start = 0.0 end = 50.0
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.