Introduction to SWIFT-structure

Source: Internet
Author: User
Tags float double

The first is to introduce the use of the basic types of swift language, today is to introduce the use of Swift's structure, the use of structure in swift and other language usage, there is not the same, but you have to knock a few times, you can understand the structure, structure in the development of iOS is a very important part of the gray, If you have a good grasp of the structure, in the later development of iOS, you will understand more clearly.

One: Structure declaration

Format: struct  structure name {} Description: 1:struct  is a keyword that defines a struct body

Example

/*1:struct is the struct's keyword 2:student struct name 3:student ()  creates a struct variable */struct  student {  }var stu = student ()//student ( ) represents the creation of a struct-body variable

Two: struct declaration field

Define struct field 2 methods struct struct  body name {First directly define field name and assign initial value to field name    var or let  field name = Initialize value The second defines the field name and specifies the field type    var or  Field name: type}

Example

struct  Student {        var age  = 0//directly defines a field name and assigns the initial value to the field name        var  name:string//directly defines a string field name.    }

Third: Access to struct declaration fields


One: Create struct variables, the structure of the field must have an initial value, otherwise it will be an error

Two: Inside a struct only the value of the field is modified in the constructor (init), and within the structure, the values of the inner fields of the struct cannot be directly modified inside the method and property.
Three: Outside the struct, struct variables, you can access the struct field, or you can modify the value of the struct field.
①: Constructor assigns an initial value description to the struct field : Specifies the struct constructor in the 1:swift language, which is the Init method 2:init execution method, executed before the struct variable is created

3: Parameters in the creation of structural body variables must be identical to the constructor init parameters. ②: Create struct variable to assign an initial value

When you create a struct variable and assign it directly to a field, note that the order in which you assign the initial value must be the same as the order in which the struct is declared.


Example

No parameter constructor

struct  student {        var  = 0//directly defines a field name and assigns an initial value of        var  name:string//directly defines a string variable.        //define parameterless constructor    Init () {        name = "ZS" Age        = 1    }    }
/*
1:student () creates a struct variable and the system automatically calls the constructor init ()
*/
println ("name=\ (Stu.name), age=\ (stu.age)") Run results name=zs,age=1

Parametric constructors

struct  student {        var  = 0//directly defines a field name and assigns an initial value of        var  name:string//directly defines a string variable.        //Define a parametric constructor    init (name:string,age:int) {        self.name = Name//self refers to the current struct-body variable    self.name The field of the variable for the current struct        Self.age = Age    }    }/*
1;student (Name: "ls", age:12) creating a struct variable system will mobilize the constructor, and the parameters for creating the struct are consistent with the constructor parameters
2:student (name: "ls", age:12) followed by the parameter name must be the same as the constructor parameter variable name


*/var stu = student (Name: "ls", age:12) //Because the constructor has 2 parameters, the parameters corresponding to the variables that create the struct are consistent println ("name=\ (Stu.name), age=\ (Stu.age) ") Run structure name=ls,age=12

Parameter constructor parameter variable plus _ usage

1: The  corresponding parameter in the object that creates the struct must be the same as the parameter corresponding to the constructor 2: If a parameter variable in the constructor is added to its corresponding parameter of the created object, the variable name is not required
struct point{    var x = 0.0    var y = 0.0    init (_ X:D ouble, _ y:D ouble) {        self.x = x        self.y = y    }
   
    }/*1:     Init (_ X:D ouble, _ y:D ouble) The corresponding variable in the constructor is preceded by _ and it does not need a variable name after the object being created. */var p = Point (10.0,11.0) println ("X=\ (p.x) y=\ (P.Y)")
   




To create a struct variable to assign an initial value

struct  student {        var  = 0  //define a field name directly and assign the initial value        var  name:string?  Define a string variable directly.           }/*------Create struct variable to assign the initial value var stu = student (age:12,name: "Ganchupo") Description: 1: Create a struct variable stu, where the value of the field age is "Ganchupo" for the value of the  name: C9/>student () brackets followed by arguments must be consistent with the order of the fields that define the structure of the struct  var  stu1 = Student (name: "Ganchupo", Age:12)   because The order in which the fields are initialized in the variable that creates the struct body and the order in which the struct declaration fields are defined is inconsistent */var stu = student (age:12,name: "Ganchupo") println ("Name=\ (Stu.name), age=\ (stu.age)") Running results name= Ganchupo, age=12

Structure method

  1: The structure can be stored directly in the method note: The structure of the method cannot directly modify the value of the field, otherwise it will be an error

Example

struct  student {        var  = 0  //define a field name directly and assign an initial value            //define struct method    func getage ()->int{        return Age    }        } /* Note: The method in the struct cannot directly modify the value of the field, otherwise it will error */var Stu = student () Stu.age = 12println (stu.age) Run result 12

struct properties

Property is primarily a Get Set method

Example

struct point{    var x = 0.0    var y = 0.0}struct  CPoint {        var p = point ()            //Declaration property, Get Set method    var gpoint :P oint{    get{        return p    }    set (Newpoint) {        p.x = newpoint.x        p.y = Newpoint.y    }}        } var p = point (x:10.0,y:11.0) var cp = CPoint () CP. Gpoint = Pprintln ("x=\" (CP. Gpoint.x), y=\ (CP. GPOINT.Y) ") Run result x=10.0,y=11.0

System provides common structure with Bool Int Float Double String etc.

Note: String is a struct type in swift language

When the struct value is passed

Later in the article, I went back to learn from the Swift language of knowledge written to form a series. Since it is a new language, it is inevitable that there are deficiencies, and you are welcome to advise me. can also add my QQ 14,360,511,081 discussion, if you have any problem, you can also directly in QQ message sent to me, I saw the first time after you reply

Finally, a summary. Send the mind map as the end of the article

Introduction to SWIFT-structure

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.