More content, welcome to pay attention to the public account: “Swift Garden”
If you like the article, please click “like” to follow it
variable
When you start Xcode, it will ask you what you want to do, select “Get Started with a Playground”. Playground is a sandbox environment where you can enter Swift code and see the results immediately.
By default, Xcode will provide a blank Playground for the iOS programming environment. Click Next and then Create to save the Playground to your system desktop.
In this article, I’ll introduce you to “variables,” which are the places you store program data. They are called “variables” because they are mutable, meaning you can freely change their value.
We’ll start with “variables” from the first line of code in Playground, which creates a string variable named “STR” with a value of “Hello, Playground”
var str = "Hello, playground"Copy the code
This line creates a new variable named STR and assigns it the value “Hello, playground.” In the output field to the right of Playground you can see “Hello, Playground” — this is Xcode showing the value we just set to STR.
Since STR is a variable, we can change it:
str = "Goodbye"Copy the code
The second time, you don’t need to write var again because the variable was created earlier — we just need to change it.
String and integer
Swift is a type-safe programming language, which specifically means that each variable must have a specific “type”. The STR variable that Xcode created for us has a String composed of the letters “Hello, playground,” so Swift assigns a String type to the variable.
In other words, if we wanted to store a person’s age, we might need to create a variable like this:
var age = 38Copy the code
This variable has an integer, so Swift gives it type Int – Int is short for “integer”. If your number is large, Swift allows you to use an underscore as a thousand separator. These separators don’t change the absolute value of the number, they just make it easier to read.
Such as:
var population = 8 _000_000Copy the code
String and Int are different types and cannot be used interchangeably. Therefore, you can change the value of STR to “Goodbye”, but you cannot change it to 38, because 38 is an Int, not a String.
Multiline string
The standard Swift string is represented by double quotes, but you cannot wrap lines between double quotes. If you want to represent a multi-line string, you need to change the number of quotes at the beginning and end of double quotes from one to three, as follows:
Var str1 = """ this string spans multiple lines """Copy the code
Swift has a special rule about writing these quotes: the first three quotes and the last three quotes must each occupy a single line, but neither line counts in the final string. If you just want to make your code look cleaner with multi-line strings, you can ensure that the newline does not enter the final string by adding a \ at the end of each line, like this:
Var str2 = """ string \ looks to span multiple lines, \ actually has no """Copy the code
Double – precision floating – point and Boolean
There are also two basic data types in Swift, which you’ll use a lot: double floating-point and Boolean.” “Double” is used to stand for “Double,” which is an advanced way of saying decimals like 38.1, or 3.141592654. Whenever you create a variable from a score, Swift automatically assigns that variable type Double.
Here’s an example:
var pi = 3.141Copy the code
Double – precision floating – point numbers are different from integers and cannot be mixed. Boolean types are simpler: they only have true and false values, so when you create a variable with true or false, Swift automatically assigns that variable a Boolean type
Here’s an example:
var awesome = trueCopy the code
String interpolation
You learned earlier how to enter string values directly into code, but Swift also provides a feature called “string interpolation.” This feature allows you to dynamically assign a value to a String variable by inserting variables into a String value. Inside a string, you can put any number of variables of any type. To place these variables, you need to start with a backslash, \, followed by the variable name wrapped in parentheses.
Here’s an example:
var score = 85 var str = "Your score is\(score)"Copy the code
As you’ll see in the output area of the Playground, the above operation sets the value of the STR variable to “your score is 85.”
Try it a few more times and compose any string you want through string interpolation.
var results = "The test results are as follows:\(str)"Copy the code
As you will learn later, string interpolation can be used not only to place variables, but also to run code.
constant
It’s useful to mention that variables are called variables because you can change them over time. However, sometimes you only need to set the value once and never change it again, in which case we use constants. Corresponding to the var keyword for variables, constants are represented by the let keyword. The let keyword is used to declare a constant whose value can only be set once and cannot be changed.
Here’s an example:
let taylor = "swift"Copy the code
If you try to change a constant’s value, Xcode will refuse to run your code. It’s a safety mechanism to prevent you from accidentally changing something that shouldn’t have been changed. When writing Swift code, you should always use the let keyword unless you explicitly want to change it. In fact, Xcode will warn you if you declare a variable with var and then don’t change it again.
Type annotations
Swift assigns variables and constants a type based on the value assigned to them when they are created. So when you write code like this, Swift knows it contains a string:
let str = "Hello, playground"Copy the code
It will treat STR as a String, so you can’t assign it an integer or a Boolean value after that. This process is called type inference. SwiftSwift is able to infer the type of something based on how you create it. If you want to explicitly declare the type of the data instead of relying on Swift’s type inference, you can write:
let album: String = "Reputation"
let year: Int = 1989
let height: Double = 1.78
let is: Bool = trueCopy the code
The abbreviation of a Boolean type is Bool, as is the abbreviation of an integer type Int.
conclusion
Let’s sum it up.
- We use var to create variables and let to create constants. If variables are not necessary, use constants whenever possible.
- The String type usually begins and ends with double quotes. If you want strings to span multiple lines, replace the quotes with three.
- Int stores integers, Double stores decimals, and Bool stores true or false.
- String interpolation allows you to create new strings by placing other variables and constants inside them.
- Swift uses type inference to assign types to variables or constants, but you can also provide types explicitly.
My official account
Here are Swift and computer programming related articles, as well as excellent translation of foreign articles, welcome to follow ~