0% found this document useful (0 votes)
82 views6 pages

Swift Notes PDF

learn swift
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views6 pages

Swift Notes PDF

learn swift
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Wednesday, 8 May 2024

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:

// This is a single line comment


/* This is a multi line comment */

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.

- Example: rstGreeting, helloWorld, heIsAGoodBoy, thereSheGoes

• 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.

In Pascal case it’s like “ThisIsWhatPascalCaseLooksLike”


In Snake case it’s like “this_is_what_snake_case_looks_like”

Booleans and Strings


• Strings means regular text. It has to be inside quotes “ ”
• In swift if we hold the ‘option’ button and click on an item then we can know what
is the type of the item.

When we are learning it is recommended to include type of our item in the code ex:

let myFirstName: String = “Hey all”

// We can reference previously created objects


Let myName = myFirstName

• Regular texts can be a constant or a variable. To represent a constant we use


“Let” to represent a variable we use “Var”
• We have to use “Let” in most of the places to avoid any kind of error by our own
mistake of reassigning the same twice.

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.

Let mySecondName: Bool = true


Let myThirdName: Bool = false

- Just like string and boolean there are dates too in swift.

- In swift we have ‘Numbers’ too which can be Int, Double, CGFloat

• ‘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’ and ‘Else’ Statement


var userIsPremium: Bool = true

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 we add “ ! ” before a text it implies ‘not’

‘ !userIsPremium ‘ this means a user is not premium.

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

• If we need to add something to like count then we will do it like this

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.

• For addition we will use +=


• For subtraction we will use -=
• For multiplication we will use *=
• For division we will use /=

In coding order of operations does matter. Her BODMAS rule is followed!

So if I type likeCount = likeCount + 1 * 5


In this case swift will rst multiply then add it with the likeCount. To avoid that from
happening we can put brackets around likeCount & 1.

likeCount = (likeCount + 1) * 5

Using ‘if’ statements with numbers:


var likeCount: Double = 5
var commentCount: Double = 10
var viewCount: Double = 100

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.”)
}

- ‘&&’ is used to join two di erent codes.


So we will use this here when we have to join like count and comment count.

If likeCount > 3 && commentCount > 10 {


print(“Post has greater than 3 likes and greater than 10 comments”)
}

- 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.

If (likeCount > 3) && (commentCount > 10) {


print(“Post has greater than 3 likes and greater than 10 comments”)
} else {
print(“Post has 3 or less 3 likes OR post has 10 or less comments”)
}

- ‘ || ’ is used as an OR statement

If (likeCount > 3) || (commentCount > 10) {


print(“Post has greater than 3 likes OR greater than 10 comments”)
} else {
print(“Post has 3 or less 3 likes AND post has 10 or less comments”)
}

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

myFirstFunction( ) after the closure bracket

How to chain two or more functions


func myFirstFunction( ) {
print(“My rst function called”)
}

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

• ‘ ?? ‘ is called Nil coalescing operator which basically means otherwise.


- We cannot set an optional value to a non optional value. Ex:

let myOptionalBool: Bool? = myBool


let anotherBool: Bool = myBool

[ This will throw us an error ]

To x that error we can use nil coalescing operator

• let anotherBool: Bool = myBool ?? True


[this statement means that type the value of my bool (if there is one) otherwise print
true]

- Optionals are not only limited to booleans they can be anything. Let’s see an
example with a string.

var myString: String? = “ Hello, world”


print(myString ?? “There is no value”)
myString = “New life”
print(myString ?? “There is no value”)
myString = Nil
print(myString ?? “There is no value”)

Here ‘??’ Will print the value if there is one otherwise it will print the optional value
inside “ ”

6

fi

You might also like