Swift Notes PDF
Swift Notes PDF
Swift
How to add comments on Xcode
- To add comments on Xcode use “//” before starting any sentence, for multi-line
comment use “/*” and press enter.
- Example:
Naming conventions
• In swift we use a naming convention called as Camel Case where the starting
letter is small and the next letter is capital.
• There are some other types of naming conventions too like Snake case, Pascal
Case but in swift we have to stick to camel case only.
When we are learning it is recommended to include type of our item in the code ex:
1

fi
- Boolean is also called ‘Bool’ which is used to put the value ‘True’ or ‘False’ we
use it when something is only going to be true or false.
- Just like string and boolean there are dates too in swift.
• ‘Int’ is a whole integer without any kind of decimal. If there is a decimal its either a
double or CGFloat. Double and CGFloat are basically same but we use double for
math and CGFloat for UI components.
if userIsPremium == true {
print(“1- user is premium”)
}
if userIsPremium {
print(“2 - user is premium)
}
• The two aforementioned texts has same meaning! Since we have already given
the value true to ‘userIsPremium’ an open bracket signi es that it’s true.
if userIsPremium == true {
print(“1- user is premium”)
} else {
print(“1.1 - user is NOT premium”)
}
• We use else when we have to print something like if this is not then type this!
2

fi
Operators
var likeCount: Double = 5
var commentCount: Double = 10
var viewCount: Double = 100
likeCount = 5 + 1
- If we write like this we will get our desired result but if the number changes from 5
then we won’t get anything. Hence this would be bad coding and we won’t do it.
likeCount = likeCount + 1
likeCount += 1
- Both the aforementioned codes will give us the same result and we can choose
which ever one suits us.
likeCount = (likeCount + 1) * 5
likeCount += 1
If likeCount == 5 {
print(“Post has 5 likes.”)
} else {
print(“Post does NOT have 5 likes”)
}
3

fi
If likeCount != 5 {
print(“Post does NOT have 5 likes”)
}
If likeCount > 5 {
print(“Post has more than 5 likes.”)
}
If likeCount < 5 {
print(“Post has less than 5 likes”)
}
If likeCount >= 5 {
print(“Post has greater than or equal to 5 likes.”)
}
- But this code will only print when both the command will be true.
And to print when both values are di erent we will use else statement.
- ‘ || ’ is used as an OR statement
4

ff
ff
Monday, 13 May 2024
Functions
Functions are denoted by ‘func’
func myFirstFunction( ) {
print(“Hey Guys What’s Up”)
}
If we run the code above it won’t print anything because we aren’t calling the
function. To run the above code we need to call that function by typing
func mySecondFunction( ) {
print(“My second function called”)
}
myFirstFunction( )
mySecondFunction( )
- To chain both the functions we will put the statement mySecondFunction inside
the closure bracket of the rst function.
func myFirstFunction( ) {
print(“My rst function called”)
mySecondFunction( )
}
5

fi
fi
fi
Tuesday, 14 May 2024
Optionals
• Nil value are when an object might have a value but might have nothing.
• ‘ ? ‘ represents an optional.
var myBool: Bool? = false
- Optionals are not only limited to booleans they can be anything. Let’s see an
example with a string.
Here ‘??’ Will print the value if there is one otherwise it will print the optional value
inside “ ”
6

fi