Learn from the zero-starting Swift learning Note (Day58) specification for variable or constant declarations of--swift coding specifications

Source: Internet
Author: User

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

A declaration is a specification that needs to be followed when declaring variables, constants, properties, methods, or functions, and custom types.

The first time a variable or constant is declared in each row, the number of variables or constants is recommended on one line, as this facilitates writing comments. The sample code is as follows.

Recommended Use:

Let level = 0var size = 10


It is not recommended to use:

let level = 0; var size =10


data for variables or constants type, and if possible, type inference should be used so that the code is concise. The sample code is as follows.

Recommended Use:

Let level = 0var size = 10


It is not recommended to use:

Let Level:int = 0var Size:int = 10


If it is not the default data type, you need to explicitly declare the data type of the variable or constant. The sample code is as follows.

Let level:int8 = 0var Size:int64 = 10


you need to use a colon when specifying a data type ( : ), size There is no space between the colon, and there is a space between the colon and the data type. The sample code is as follows.

Recommended Use:

Let level:int8 = 0var Size:int64 = 10


It is not recommended to use:

Let level:int8 = 0var Size:int64 = 10


Use as much as possible when working with data types Swift The data type itself, for example:

Recommended Use:

Let width = 120.0 let widthstring = "Hello." var devicemodels: [String]var employees: [Int:string]


It is not recommended to use:

Let Width:nsnumber =120.0 let widthstring:nsstring = "Hello." var Devicemodels:nsarrayvar employees:nsdictionary


Property declaration

properties include storage properties and computed properties, if the declaration specification for a stored property is the same as the specification of a variable or constant declaration. If the computed property is similar to a code block, when you use a read-only computed property, you may want to omit the get statement. The sample code is as follows.

Recommended Use:

var fullname:string {return firstName + "." + LastName}


It is not recommended to use:

var fullname:string {get {return firstName + "." +lastname}}


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://s2.51cto.com/wyfs02/M01/7C/F2/ Wkiom1bdaioim7h9aaas2mbeznc970.png "alt=" Wkiom1bdaioim7h9aaas2mbeznc970.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/1748344

Learn from the zero-starting Swift learning Note (Day58) specification for variable or constant declarations of--swift coding specifications

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.