On those structures and enumerations in swift

Source: Internet
Author: User

First of all, we'll write a struct.

struct Point {    0    1    mutating func-Change (newx:int,newy:int) {        = newx        = newy    }}

var P1 = point (X:4, Y:4)

p1.x  //4

Here, one might ask why a mutating is added in front of the Func,

If not added, the compiler throws an error, reporting that the property value cannot be modified in the instance method

So, we have to add a mutating in front of the func if we want to change the member variable.

The thing to note here is that a struct is a value type

Let P = Point ()

p.x = 20//Error

p = point ()//error, let action

enumeration defines a common group type for values associated with a series. It also allows you to use these values in a type-safe situation when you are programming.

You can define an enumeration by starting with an enum and enclosing the entire definition body with curly braces:

Enumcase case   }

First, let's start by creating an enumeration

enum Gender {    case  Male (String)    case// indicates that any type can be associated, The data associated with each case can be different }

We instantiate an enumeration object

Let WD = Gender.male (" male ")

Switch to judge

Switch WD { case . Male (Let x):    print (" this is \ (x)")  //"This is a man"case  . Female:    print ("NV")}

You can also instantiate two parameters

Let hy = Gender.female ("zs")

Switch a bit.

Switch hy { case . Male (Let x):    print ("zhe shi ge \ (x)")case// the equivalent of x and Y arelet    print ("nv\ (x, y)")  //" NV (18,ZS) "}

Well, the enumeration is here to the end of this,

And to add, what about? And! Chained operation ()

var i:string?= "ddd" and var i:string!= "ddd" are optional

? Indicates that the value can be NI, if the general data type is not allowed for nil, in chained operation if it is nil, no error

! Called implicit solution, forced conversion, in the chain operation if it is nil, the direct error

Let's write a case to distinguish between the two, first of all, if you do not assign a value, it is the default is nil

var a:string?           // A is nilvar b:string!           // B is nilvar a_test = a            //a_test is nilvar b_test = b            //b_test is nil

We define two string variables

" ASDFASDF "
var str1:string! = "ASDFASDF"

We're going to call the string method

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

Chained Operation Example

classPerson {var ci:classinfo?   }classClassInfo {var t:teacher?}classTeacher {var name="CJ"}let P=Person () let T=Teacher () Let CI=ClassInfo () ci.t=Tp.ci=Cip.ci?. T?. Name ="Double"P.ci?. T?. Name//"Double"p.ci!. T!. Name//You can force the "double" to be unpacked

Summarize

      1. Question mark?
        A. Add when declaring? , tells the compiler that this is optional, and automatically initializes it to nil if the declaration is not manually initialized
        B. Before the variable value operation is added? To determine if the variable is nil, it does not respond to the subsequent method.
      2. Exclamation mark
        A. Add when declaring! , tell the compiler that this is optional, and then, after the operation of the variable, it is implicitly added before the operation!
        B. Before you add to the variable operation! , which means that the default is non-nil and the package is processed directly

On those structures and enumerations in swift

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.