Swift's structure, enumeration, optional optional, optional chain

Source: Internet
Author: User

: Playground-noun:a place where people can play

Import Cocoa

var str0 = "Hello, playground"

Classes are reference types, structs and enumerations are value types

Structural Body ***************

struct Point {

var x = 0

var y = 1

The method of the structural body

To be able to modify property values in an instance method, you can add a keyword before the method definition mutating

Mutating func Change (newx:int,newy:int) {

x = newx

y = Newy

}

}

var p = point ()

P.change (1, Newy:2)

p.x

P.y

Automatically adds an initializer for you with all members, Memberwise Init

var P2 = point (X:5, Y:6)

p2.x

Let P3 = point ()

P3 = point () error, let definition of P3 is constant pointer

p3.x =20 struct is a value type, so also error

Enumeration ************

Enum Gender {

Case Male

Case Female

}

Use of enumerations

Let Ch:gender =. Male

You can also define this

Let CH2 = Gender.male

Switch CH {

Case. Male:

Print ("male")

Case. Female:

Print ("female")

}

The associated value of the enumeration *********

Enum Gender2 {

Case Male (String)

Case Female (int,string)//() can be associated with any type, each case associated with the data can be different

}

Let CH3 = Gender2.male ("male")

Switch CH3 {

Case. Male (let C):

Print ("Gender is \ (c)")

Case. Female:

Print ("Sex Girl")

}

Let CX = Gender2.female (11, "female")

Switch CX {

Case. Male (let C):

Print ("Gender \ (c)")

Case. Female (Let C,let x):

Case Let. Female (C,X)://Here if c,x are let, you can define

Print ("Gender \ (x), age \ (c)")

}

Native value of the enumeration ***********

The native value, if you do not assign it to the value, it is starting from 0, think of the following x = 1, because it is the default starting from 0, that approved equals 1, that violates the original is worth uniqueness, so error. Assuming x = 3, then y will be equal to 4, and if there is a case, 5,6,7,8 ... keep going like this.

Enum Status:int {//All of the native values must be unique

Case Unapproved

Case approved

Case x = 3//x = 1 Error

Case Y

}

Let S = status.x

S.rawvalue

Let S2 = Status.y

S2.rawvalue

S2.hashvalue//Display the original value of the previous case

Optional optional

var str:string? = "Chenhe"//Plus? After Str is a string that can be set to nil

str = NIL;

Let i:int?//add? No value is assigned to it after the default is nil

Str?. startindex//Remove Nil Value

Str.startindex will error, because STR is not a true string type at this time, so there is no startIndex

In this case, there are two ways to solve the optional problem

First Kind

Str!. StartIndex

Str!

The second Kind

If let S = str {

Print ("\ (s)")

}else {

Print ("str value is nil")

}

Simple notation

var str2:string! = "Chenxuan"//implicit unpacking

STR2 = Nil

When STR2 is assigned nil, the str2! output value is nil and no assignment nil outputs the string content

Optional chain optional chain ******************

Multi-level optional chain

Class Person {

var ci:classinfo?

}

Class ClassInfo {

var t:teacher?

}

Class Teacher {

var name = "CJ"

}

var p0 = person ()

var ci = ClassInfo ()

var t = Teacher ()

P0.ci = CI

CI.T = t

P0.ci?. T?. Name = "Chenhe"

P0.ci?. T?. Name

p0.ci!. T!. Name

If Let n = P0.ci?. T?. Name {//can also be used with optional bindings

Print ("\ (n)")

}

Swift's structure, enumeration, optional optional, optional chain

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.