Swift getting started-structure

Source: Internet
Author: User
Tags float double

The previous section mainly introduced the basic usage of the swift language. Today we will introduce the usage of the swift struct. The usage of the struct in swift is different from that in other languages, however, you can understand the struct several times. The struct is a very important part in ios development. If you have a good grasp of the struct, will understand more clearly.

 

I. struct Declaration

Format: struct name {} Description: 1: struct is the keyword that defines the struct.

Example

/* 1: struct is the keyword of the struct 2: student struct name 3: student () create a struct variable */struct student {} var stu = student () // student () creates a struct variable.

2. struct declaration Fields

Two Methods for defining struct fields: struct name {the first method is to directly define the field name and assign the initial value var or let field name = initialization value to the field name. The second method is to define the field name and specify the field type var or let field name: type}

Example

Struct student {var age = 0 // define a field name directly, and assign the initial value var name: String // to the field name to define a String field name directly. }

 

3. Access to struct declaration Fields


I. Create struct variables. All struct fields must have an initial value. Otherwise, an error is reported.

2. Only the Field Values of the struct can be modified in the constructor (init). The values of the internal fields of the constructor cannot be directly modified in the constructor's methods and attributes.
3. Outside the struct, the struct variable can access the struct field or modify the value of the struct field.
①: The constructor assigns an initial value to the struct field. Description: 1: Specifies the struct constructor in swift. It is the init Method 2: init method. It is executed before the struct variable is created.

3: the parameters of the variable created for the struct must be consistent with the init parameter of the constructor. ②: Create a struct variable to assign an initial value

Create a struct variable and assign it directly to the field. Note that the order of initial values must be the same as that of declared struct.


Example

No-argument Constructor

Struct student {var age = 0 // define a field name directly and assign the initial value var name: String // define a String variable directly. // Define the non-parameter constructor init () {name = "zs" age = 1 }}
/*
1: student () creates a struct variable. The system automatically calls the constructor init ()
*/
var stu = student ()
Println ("name = \ (stu. name), age = \ (stu. age)") running result name = zs, age = 1

Parameter Constructors

 

Struct student {var age = 0 // define a field name directly and assign the initial value var name: String // define a String variable directly. // Defines the parameter constructor init (Name: String, Age: Int) {self. name = Name // self indicates the current struct variable self. name: The field self of the variable of the current struct. age = Age }}/*
1; student (Name: "ls", Age: 12) the system will mobilize the constructor to create the struct variable, and the parameters for creating the struct are consistent with those of the constructor.
2: student (Name: "ls", Age: 12) must follow the same Name as the variable Name of the constructor parameter.


*/Var stu = student (Name: "ls", Age: 12) // because the constructor has two parameters, the parameters of the variables corresponding to the created struct must be consistent with println ("name = \ (stu. name), age = \ (stu. age) ") Running structure name = ls, age = 12

Parameter Variable addition with parameter Constructor

1: The corresponding parameters of the object that creates the struct must be the same as those of the constructor. 2. If the prefix of the parameter variable in the constructor adds _ it corresponds to the parameter of the object to be created, no variable name is required.
Struct Point {var x = 0.0 var y = 0.0 init (_ x: Double, _ y: Double) {self. x = x self. y = y }}/ * 1: init (_ x: Double, _ y: Double) in the constructor, add _ before the corresponding variable. No variable name is required after the corresponding object is created. */Var p = Point (10.0, 11.0) println ("x = \ (p. x) y = \ (p. y )")

 




Create a struct variable to assign an initial value

Struct student {var age = 0 // define a field name directly and assign the initial value var name: String? // Define a string variable directly. }/* ------ Create a struct variable to assign the initial value var stu = student (age: 12, name: "Gan chaobo") Description: 1: Create a struct variable stu, note: student () the parameters following the brackets must be consistent with the order of fields defining the struct. var stu1 = student (name: "Gan chaobo", age: 12) the initialization field order in the variable that creates the struct is inconsistent with that in the field declared by the struct definition */var stu = student (age: 12, name: "Gan chaobo ") println ("name = \ (stu. name), age = \ (stu. age) ") running result name = Gan chaobo, age = 12

 

Struct Method

1: You can directly store the methods in the struct. Note: The methods in the struct cannot directly modify the field value; otherwise, an error is reported.

Example

Struct student {var age = 0 // define a field name directly and assign the initial value // define the struct method func GetAge ()-> Int {return age}/* Note: the method in the struct cannot directly modify the field value. Otherwise, an error is returned */var stu = student () stu. age = 12 println (stu. age) running result 12

Struct attributes

The attribute is mainly the get set method.

Example

Struct Point {var x = 0.0 var y = 0.0} struct CPoint {var p = Point () // declare the attribute, get set method var GPoint: point {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) ") running result x = 10.0, y = 11.0

The system provides common struct types such as Bool Int Float Double String.

Note: In SWift, String is a struct type.

 

When the struct value is passed

 

 

In the subsequent articles, I wrote the swift language I learned to form a series. Because it is a new language, it is inevitable that there are deficiencies. Please give me your comments. You can also add QQ 1436051108 for discussion. If you have any questions, you can also send a message to me via QQ. I will reply to you immediately after seeing it.

Summary. Send a mind map to end the article

 

 

 

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.