Swift constants and variables and naming rules

Source: Internet
Author: User

In the previous chapter, we introduced how to use swift to write a helloworld applet, where variables are used. Constants and variables are important components of expressions.
Constant
When declaring and initializing a variable, you can add the keyword let before the identifier to specify this variable as a constant. As the name suggests, a constant is the amount of constant that does not change during use. The instance code is as follows:
Let_hello = "hello"
_ Hello identifier is a constant and can only be assigned a value during initialization. If we assign a value to _ Hello again, the Code is as follows:
_ Hello = "Hello, world"
The program reports an error, as shown in. The error message is displayed in the timeline.
 
From the error message, we can know that _ Hello is the value assigned by let and cannot be assigned a value.
Variable
To declare a variable in swift, add the keyword var before the identifier. The instance code is as follows:
VaR scoreforstudent = 0.0
This statement declares the double-type scoreforstudent variable and initializes it to 0.0. If multiple variables are declared and initialized in a statement, all variables have the same data type:
VaR x = 10, y = 20
In the Declaration of multiple variables, we can also specify different data types:
VaR x = 10, y = true
X is an integer and Y is a Boolean integer.
Naming rules
When using constants and variables, ensure that their names comply with the specifications so that the program can have good readability. This is also a good programming habit.
1. Constant name
Constants of the basic data type are all in uppercase. If they are composed of multiple words, they can be separated by underscores (_). For example:
Let year = 60
Let week_of_month = 3
2. variable name
Variables are named in multiple styles and are mainly clear and easy to understand. For convenience, some programmers use a single letter as the variable name, such as J and I, which will cause difficulties for program maintenance in the future and increase the probability of variable names with the same name. A single letter variable is generally used only for loop variables, because they only apply to the body of the loop.
In the past, computer languages used to limit the length of variable names, but no such restriction exists. Therefore, we encourage clear names to indicate the role of variables, it usually starts with a lower-case letter, and the first letter of other words is capitalized, for example:
VaR maximumnumberofloginattempts = 10
VaR currentloginattempt = 0
Such a name can make the role of a variable clear at a glance.
In addition to naming constants and variables, the names of objects in other languages must also be standardized. The naming rules for the class name, protocol name, struct, enumeration, and Other types are generally starting with an uppercase letter, and the first letter of other words is capitalized, such as the class name helloworldapp.

Function and method names are often combined by multiple words. The first word is usually a verb and starts with a lowercase letter. The first letter of other words is capitalized, such as balanceaccount and isbuttonpressed.

 

 

 

For more information, please refer to the first domestic swift book "swift development guide" for discussion. Website: http://www.51work6.com/swift.phpwelcome to the swifttechnology discussion group: 362298.pdf

 

 

Welcome to Zhijie IOS public classroom Platform

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.