Learn notes from scratch (Day 25)-Class and struct definitions

Source: Internet
Author: User

original articles, welcome reprint. Reprint Please specify: Dongsheng's blog

Swift The syntax for classes and struct-body definitions in is very similar. Classes use class keywords to define classes, and struct keywords are used to define structs, and their syntax is formatted as follows:

Class class Name {Defines a member of a class}struct struct name {Defines a member of the struct body}


from the syntax format, Swift The definitions in the classes and structs in are more similar to the Java Grammar, does not need to be like C + + and the objective-c in this way, the interface and implementation parts are placed in different files.

Let's look at an example:

Classes defined by class employee {             //     var no: Int = 0                var name: String =  ""          var job: String?               var salary: Double = 0              var dept: Department?          } struct department {          // Defined structure Body     var no: Int = 0                var name: String =  ""      }


Some attributes are defined inside.

Employee and the Department It's about the relationship. .

The following statements are instantiated:

Let emp = Employee () var dept = Department ()


Employee () and the Department () is to instantiate the constructor that calls them.

Note: The class is declared as Let constant is still var What about the variables? The class is generally declared as a Let constant from the programming process, because the class is a reference data type, and declaring that a let constant simply means that the reference cannot be modified, but the object that the reference points to can be modified.

Welcome to follow Dongsheng Sina Weibo@tony_Dongsheng.
Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom
650) this.width=650; "title=" 00.png "src=" http://s1.51cto.com/wyfs02/M00/7C/AB/wKioL1bVaQPCD2hMAAAs2MBEZnc161.png "alt=" Wkiol1bvaqpcd2hmaaas2mbeznc161.png "/>
More ProductsIOS,Cocos, mobile Design course please pay attention to the official website of Chi Jie Classroom:http://www.zhijieketang.com
Smart-Jie Classroom Forum Website:http://51work6.com/forum.php


This article is from the "Dongsheng-ios Technical Consultant" blog, make sure to keep this source http://tonyguan.blog.51cto.com/701759/1746423

Learn notes from scratch (Day 25)-Class and struct definitions

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.