One of Swift Learning: constants and variables

Source: Internet
Author: User



Is learning Swift, read the English document, now is looking at the second time, by the way summed up some knowledge points. If there is a place to be questioned, please leave a message/reply to point out, thank you!






Constants and variables will have an associated name (such as maximumnumberofloginattempts or welcomemessage),


There are specific types (such as the number 10 or the string "Hello"). The value of a constant cannot be changed once it is set, and the value of the variable
In the future, it can be changed to a different value.








declaring constants and variables



Constants and variables must be declared before they are used. Use Letkeyword to declare constants, whereas variables are declared using Varkeyword.






Look at the following examples:

// The semicolon can be added or not, assuming you are not used to it, you can add it yourself
// Declare a constant named maximumNumberOfLoginAttempts, because there is no type specified. It was initialized again and its value was 10.
// So swift will actively determine the type of the constant maximumNumberOfLoginAttempts is Int
// This is equivalent to: let maximumNumberOfLoginAttempts: Int = 10
let maximumNumberOfLoginAttempts = 10
// The declared currentLoginAttempt is a variable. The value of this variable can be changed elsewhere
// The type of the specified variable is not shown here, but because of the initial value of 0, Swift will determine that the type of this variable is Int
// So the following is equivalent to: var currentLoginAttempt: Int = 10
var currentLoginAttempt = 10

Ability to declare multiple variables on one line, separated by commas
// Swift uses the Double type by default when determining the floating-point type. The following is equivalent to:
// var x: Double = 0.0, y: Double = 0.0, z: Double = 0.0
var x =


NOTE
Suppose you don't want a stored value to change in your code. Then always use letkeyword as a constant.
Use variables only for values that need to be changed.




Type Annotation
The type specification has been used in the above gaze, in fact, it displays the type of the specified variable or constant at the same time when the variable or constant is declared
// This way of writing is wrong, because we did not specify the type. And swift has no detailed initial values to infer
// We want the type of this variable
var welcomeMessage // Error

Then we have two ways to deal with it. One is to give an initial value to let swift help us infer the type, and the other is that we display the specified type
var welcomeMessage: String // What we want is String type
var welcomeMessage = "Hello" // swift will infer the type as String based on this initial value
NOTE
In actual work, it is very rarely necessary to write a type description. Assuming that the initial value is provided when the variable or constant is defined,
Then Swift will always judge the type of the variable or constant.


Named constants and variables
Unicode characters can be used in swift
let n = 3.14159
Hello let = "Hello, Swift" // Although Chinese can be used as a variable name. But I don't recommend it.
. .

personal suggestion



Print constants and variables
// Print statements in swift can use println, print
// The former is a newline added at the end of printing. The latter did not join
println (welcomeMessage) // Will wrap
print (welcomeMessage) // Will not wrap

// Did you notice the way of printing here? Use \ () to put the variable in parentheses
println ("The welcomeMessage is \ (welcomeMessage)")
The


One of Swift learning: constants and variables


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.