Swift Again Flashcards
Variables and Constants
Constants and variables associate a name with a value of a particular type.
Constant: let
Variable: var
Constants cannot be changed
Variables can be changed
How do you declare multiple constants or variables
Separate them by comma
var x = 6, y = 10
Type annotation
Used as a way to be clear about what type of values a variable or constant can store.
To do this, place a colon after constant or variable name.
For example:
var thisExample: String
How do you store multiple type annotations in one line?
Separate them by commas.
For example:
var red, green, blue: double
What can’t a variable name be?
Whitespace characters Mathematical symbols Arrows Private-use (or invalid) Unicode code-points Box drawing characters
They also can’t start with a number.
String Interpolation
Combining two or more variables while adding a new string
Use backslash to input existing variables without severing the string For example: let bananas = 5 let apples = 16 let oranges = 7 print("I am shopping for \(bananas) bananas, \(apples) apples, and \(oranges) oranges. In total, I will buy \(bananas + apples + oranges) pieces of fruit.")
The result:
I am shopping for 5 bananas, 16 apples, and 7 oranges. In total, I will buy 28 pieces of fruit.
What are comments?
Pieces of information I can add to tell me what my code does.
Use // to create a comment
Use /* create a comment
over multiple lines /*
What are nested comments?
/* Comments that started as multiline /* and become nested */ And then you can add a new comment to end the first multiline comment */
Use of semicolons in Swift
Not required in the same way it is in other languages, but it is necessary in order to in order to write multiple separate statements like:
let catDog = “fun”; print(catDog)
Integers
Whole numbers
Signed Integers
Unknown definition, but Swift provides
6, 16, 32, and 64 bit forms
8-but unsigned integer is a UInt-8
32 signed integer is Int32
Integer Bounds
The minimum and maximum values of each integer type with its min and max properties.
Example:
let minValue = UInt8.min // minValue is equal to 0, and is of type UInt8 let maxValue = UInt8.max // maxValue is equal to 255, and is of type UInt8
The values of these properties are of the appropriate-sized number type (such as UInt8 in the example above) and can therefore be used in expressions alongside other values of the same type.
Int
In most cases, it’s not necessary to pick a certain size for your integer. Int is an additional integer type that has the same size as the platforms current native word size.
For example:
On a 32-bit platform, Int is the same size as Int32.
On a 64-bit platform, Int is the same size as Int64.