Day 1 - Swift Variables, Constants, Strings, and Numbers
Table of Contents
- Variables are created with
var
and constants are created with the keywordlet
. - The value of variables marked as
var
can be changed anywhere in the program. - Variables marked as
let
cannot be modified once their values have been assigned.
Defining Variables and Assigning Values #
var name = "Ted" //Variable definition and value assignment
name = "Rebecca" //Change value
name = "Keeley" //Change value
let character = "Daphne" //A constant is defined and assigned a value.
character = "Eloise" //It will give an error!!! Because a constant
character = "Francesca" //It will give an error!!! Because a constant
As a general rule, the “camel case” style should be used when naming variables. In this style, the naming starts with a lower case letter, the second word is adjacent and capitalized.
let managerName = "Michael Scott"
let dogBreed = "Samoyed"
let meaningOfLife = "How many roads must a man walk down?"
Creating a String #
A String can be created by assigning text to a variable or constant. String expressions begin and end with double quotes ("). You can use punctuation marks, emojis and other characters in String expressions.
let actor = "Denzel Washington"
let filename = "paris.jpg"
var result = "⭐️ You win! ⭐️"
Here we encounter a problem. When we want to use the double quote ("") character in a String, we will get an error, because Swift will not be able to recognize where the String expression ends and begins. To avoid this, we use backslash ().
let quote = "Then he tapped a sign saying \"Believe\" and walked away."
//OUTPUT : Then he tapped a sign saying "Believe" and walked away.
There are no character limitations when creating string expressions, but writing very long lines significantly reduces readability. Swift has a solution for this too: Split String expressions into multiple lines.
//AN INCORRECT EXAMPLE XCODE WILL NOT BE ABLE TO COMPILE IT.
let movie = "A day in
the life of an
Apple engineer"
let movie = """
A day in
the life of an
Apple engineer
"""
As you can see from the code example above, this operation can be done by using """
3 double quotes. It should be noted that the 3 quotes must be on a single line and the String to be written must be written between these 3 quotes.
Some Methods Available with Strings #
.count #
Returns the number of all characters in the string, including spaces.
print(actor.count) //Denzel Washington
//OUTPUT : 17
.uppercased() #
Returns a String expression with characters in uppercase.
print(result.uppercased()) //"⭐️ You win! ⭐️"
//OUTPUT : "⭐️ YOU WIN! ⭐️"
.hasPrefix() and .hasSuffix() #
The .hasPrefix()
method checks if the String expression typed into it is at the beginning of the String it checks.
print(movie.hasPrefix("A day"))
//OUTPUT : true
The .hasSuffix()
method checks whether the String expression typed into it is at the end of the String it checks.
print(filename.hasSuffix(".jpg"))
//OUTPUT : true
Integer and Double Numbers #
Integer Numbers #
Whole numbers can be stored in Integer variables.
let score = 10
//or
let reallyBig = 100000000
When writing multi-digit numbers, _ can be used to make them easier to read.
let reallyBig = 100_000_000
Arithmetic operators can be used to perform arithmetic operations on integers.
let lowerScore = score - 2
let higherScore = score + 10
let doubledScore = score * 2
let squaredScore = score * score
let halvedScore = score / 2
print(score)
Instead of writing counter = counter + 5
, we can use the +=
operator. (It is possible to use these operators in all 4 operations (+=
, -=
, *=
, /=
))
The function .isMultiple(of:)
can be used to find out if an integer is a multiple of another integer.
let number = 120
print(number.isMultiple(of: 3))
//OUTPUT : true
Double Numbers #
Decimal numbers can be stored in double variables. Arithmetic operations can be performed with decimal numbers in the same way as with integers. However, it should be noted that you cannot perform arithmetic operations with an integer and a double number.
The following code block will give an error. Because you cannot add an Int
number with a Double
.
let a = 1
let b = 2.0
let c = a + b
We can take 2 different approaches to do this.
In the code below, the variable b, which is Double
, is converted to Int
. (Remember; the value of b was 2.0
).
let c = a + Int(b)
Or an Int
variable a can be converted to a Double
number.
let c = Double(a) + b
In Swift, the type of the variable can be specified when the first value of the variable is given. Afterwards, the type does not change. For example, a variable defined as String
cannot be assigned an Int
value later.
var name = "Nicolas Cage"
name = 57 //THIS ERROR !!!
In the future, when we start working with some libraries, we will see that some APIs use CGFloat
to store decimal numbers. But Swift allows us to use Double
instead of CGFloat
.
You can also read this article in Turkish.
Bu yazıyı Türkçe olarak da okuyabilirsiniz.