Swift constants and variables and naming rules, swift constant variables

Source: Internet
Author: User
Tags custom name

Swift constants and variables and naming rules, swift constant variables
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



Naming rules for variables and constants. What is a camel naming method? What are the keywords that define variable constants?

The camel-style command method, as its name indicates, refers to the name of a variable and function, which is composed of uppercase and lowercase letters. For example, this function uses the camel name method printEmployeePaychecks ();
To define the keyword of a variable constant, you need to check the language used.

Naming rules for variables and constants in vb (specific points)

When naming a programming element, follow the following naming rules:
· The first character of the name must be a letter or Chinese character.
· Spaces, periods (.), and exclamation points (!) cannot be used in the name (!) , @, &, $, #, And other characters with special meanings.
· It cannot exceed 255 characters, and the names of controls, forms, classes, and modules cannot exceed 40 characters.
· Do not have the same name as an existing keyword. Otherwise, there will be a lot of trouble in programming.
· In addition to the VB naming rules, a custom name must also reflect the meaning of the programming elements it represents.

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.