Swift Structural Body

Source: Internet
Author: User



Structs and enumerations are similar to comparable studies: http://blog.csdn.net/lwjok2007/article/details/47975415


Structs are a custom type that is similar to enumerations but different from enumerations. Simply understand that a struct is a special class that has only attributes and no methods. A property in a struct is an object that has to be owned. For example, we define a student's body, including the school number, name, class. Then a student type of object will contain the school number, name, class all the information. If a variable is declared as an enumeration type, then he can only be one of the values in the enumeration.



1 Declaration/Declaration structure of struct body is generally used for the keyword struct. We look at an example, declaring a student struct struct Student {var    studentid:int;//number    var studentname:string;//name    var studentclass: string;//class}//above declaration method we can see the data type that directly specifies each property. Another way is to not specify the data type, but directly assign value, the system automatically according to the assignment of data type struct Student1 {    var studentid=2007001    var studentname= ""    var The construction method of studentclass= ""}//2 structure//2.1 Null parameter construction method var zhangsan=student1 (); Because the struct must have an initial value, the null parameter construction method only applies to structs with default values. 2.2 Full parameter construction method var zhangsan1=student (studentid:2001, Studentname: "Zhang San", StudentClass: "Third grade")//3 structure of the value and assignment//structure of the value and assignment is very simple, Direct use. Syntax println (zhangsan1.studentname) zhangsan1.studentname= "John Doe" println (zhangsan1.studentname)//4 Finally we simply say that structs can be nested. For example, the student information may have a teacher's information, we can directly set the class teacher directly into a structure, then the student structure inside there will be a struct struct Teacher {    var teachername= "Miss Wang"    var teachersex= "female"}struct Student2 {var    studentid:int;//No.    var studentname:string;//name    var studentclass: string;//class    var teacherinfo:teacher;//head teacher Information}

Apple Development Group: 414319235 Welcome to join the Welcome discussion question

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Swift Structural Body

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.