Swift_ Enumeration | Nullable type | Enumerate Associated values | Enumeration Recursion | The concept of a tree

Source: Internet
Author: User

Nullable type

var Demo2:we_demo = Nil

The syntax for this code string above is wrong.

Why, then?

In Swift, the default value of all properties defined by a type cannot be nil

Whether it's a normal simple value type or a reference type

Then I'm going to make this property default value is null, nil

What to do, very simple, with syntax, when defining this property, declare one after the type?

This means that this property can be a nullable type in addition to the default value of the specified type

In Java, the most common type of error is nullpoinexecption,

Why is there a null attribute to assign a null value to a property?

The properties in Java are defined.

The value may be an indeterminate, not specific, impractical data

So, in Java, defining a property is going to give an initial value of NULL

In the back, give a new value.

Types that can be empty

Defines the type of the attribute after the add?, which indicates that the property is nullable type property

Swift code:

var zsname:string? = Nilzsname = "Alien" Zsname = "1002" zsname = Nilzsname = "Mars man" zsnamezsname = Nilzsname = "John Doe" zsname!if let d = zsname{    p Rint (d)}

When defining a property, add it after the type, indicating that the default value for the property can be null nil

Nil in Swift can be a null type and is packaged

Swift has made a wrapper for this type

When using this property, separate the attribute from nil

When using a property with a default value that can be empty, add it after the property!

Enumeration

The definition of an enumeration is a type that can have a

Defines an enumeration of color

Swift code:

Enum Color  : string{case    Red case    Blue case    Green}

Define a constant save a case

Swift code:

Let color = Color.green

Use switch to make multi-branch judgments

Determine if the situation exists in the color show

Do the right thing if you exist

Swift code:

The switch color {    //In the color enumeration,    //Defines the case in 3    //Red Blue Green//is the one that    determines whether color is a color enumeration of red cases    . Red:        print ("red")    //Determines if color is a green case in the color enumeration    . Green:        print ("green")    //default    :        print ("no")//        *     Color enumeration is saved in 3 cases     In the above switch judgment only listed 2 kinds to make the judgement     so, in the judgment body must have a default    *//        If you do not want to specify a default    //will be enumerated in the color 3 in the case of all the enumeration to determine/ Case    . red://        print ("red")//Case    . green://        print ("green")//Case    . blue://        print ("Blue")}

Enumerates native values (RawValue)////////////

RawValue keywords

Using the RawValue keyword to enumerate native worthwhile actions

PS native worth the premise of the operation, the enumeration must be of type string and int to

Swift code:

Define an enumeration Scoreenum score:int{case    A case    b case    C}//Access enumeration Scorelet score_a = Score.init (rawvalue:0) Let score _b = Score.init (rawvalue:1) Let Score_c = Score.init (rawvalue:2) score.init (rawvalue:6)

/*

Summary:

In the practice of the above code, the case of enumeration is native worth the initial value is 0

Call the Init method of the enumeration, pass an enumeration of native values, and you can get the case where the native value corresponds in the enumeration

PS Invoke enumeration Enum method Init, if the original value passed RawValue in the enumeration corresponds to the case is undefined

The Init method returns nil

*/

The default initial value for an enumeration of type int is worth 0

Native values can be customized

The following code exercises an enumeration of type string

Swift code:

Defines an enumeration classenum class:string {case    name = "Tom" Case Age    }//Access and manipulate enumeration ClassClass.name.rawValueClass.init ( RawValue: "Name") Class.init (rawValue: "Age") Class.init (rawValue: "Sex")

/**************************************************

Summarize:

1>

An enum of type int, string, has a native value

Also, the native value has an initial value,

Enum native Value initial value of type int is 0

Enum native Value initial value of string type, what is the case defined in the enumeration, and what is the initial value of the enumeration native value

2>

However, the enumeration native values have an initial value,

The initial value can also be defined by itself

Case Casename = 123

Case casename = "ZS"

Specify a custom native value for the enumeration case with an equal sign after the condition name

What type is the PS enumeration, and what type is the native value of the custom enumeration case?

3>

Use of enumerations

Enumname.casename,[gets the case in the enumeration]

Enumname.casename.rawvalue,[gets the native value of the enumeration case]

Enumname.init (rawValue: "")

Enumname.init (rawvalue:2333) [Get Case of enum native value]

**************************************************/

Enumerate associated values//

Defines an enumeration of student

Swift code:

Enum Student {case    name (string) Case age    (Int) Case    Sex (string) case    test (string,int)}// The operation uses Student this enumeration student.name ("Tom") student.age ("the") Student.sex ("Demon")

/***********************************************************

In an enumeration, each case can have an associated value, and the associated value can have more than one, and the type can be different

Specify the type of the associated value with parentheses

***********************************************************/

That's a super point.

Recursive enumeration///

Let's start with a test.

Learn what is good, first get an introduction to take care of it first

It's not hard in the back.

Action>>>

To define a simple recursive enumeration

Syntax: If the defined enumeration is a recursive enumeration, you need to use the INDIRECT keyword before the keyword that defines recursion

The indirect keyword is used to identify that the current enumeration is a recursive enumeration

/*

Recursive features:

themselves call themselves

*/

Swift code:

Indirect enum recurrenceenum{case    A (String) case    D (int.) case    T (recurrenceenum)}// Use this recursive enumeration RECURRENCEENUM.A ("I am recursive enumeration of Case_a") RECURRENCEENUM.D (RECURRENCEENUM.A ("Call Yourself") recurrenceenum.t ("self")

/***********************************************

to really understand and be proficient in this recursive enumeration

It technology < tree > concepts are important and important, just understand this < tree >

Recursive enumeration This knowledge point only takes 3 minutes!!

< trees >

To explain in simple words:

Himself [a father, a mother]

Father [Father's father, mother of father]

Mother [Mother's father, Mother's mother]

Keep going back.

This is the tree.

More concise words to describe:

Computer domain, the tree has only one root node, and must have a root node, otherwise it becomes "graph" this data structure

***********************************************/

Define an enumeration and call yourself

Write an enumeration that can be used to represent an operation

Note that this is a recursive enumeration,

Using the INDIRECT keyword

Swift code:

Indirect enum operation{case    Num (int.) Case    Additive (left:operation,right:operation)    case Multiplication (left:operation,right:operation)}//5 * (3+9)//5let exp5 = Operation.num (5)//3let exp3 = Operation.num (3) 9let EXP9 = operation.num (9)//3+9let exp3joinexp9 = operation.additive (LEFT:EXP3, RIGHT:EXP9)//Integrated Let Finalexp = Operat Ion.multiplication (LEFT:EXP5, RIGHT:EXP3JOINEXP9)

________________________________over

Swift_ Enumeration | Nullable type | Enumerate Associated values | Enumeration Recursion | The concept of a tree

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.