0% found this document useful (0 votes)
10 views8 pages

Swift Deep Dive Notes

Notes class
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)
10 views8 pages

Swift Deep Dive Notes

Notes class
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/ 8

Swi$ Deep Dive

PART I
Naming Conven,ons
1. Camel casing
-cameCase
2. Kebab casing
-kebab-case
3. Snake casing
-snake_case

Commen,ng
//This is a single line comment
/*This is a mul=ple line comment*/

Print Statement
print(Something to print)

PART II
Variables
-simply a way of giving a name to a piece of data so that we can reference it to our code
later on.

Eg. var = data

Arrays
⁃ Set of data e.g. [ Angela, Jack, Philip] [x]

PART III
Constants
-cannot be changed throughout the program
E.g. let nameData = value
Dic=onary
let nameData = [“Name”: value, “Name”: value]
Basic data
1. String - simply a text

2. Integer - whole number


E.g Int( )

3. Float - not a whole number but less accuracy


E.g float( )

4. Double - more accurate than float


E.g Double( )
*var.dropLast - convert numbers with string format (e.g. 20%) to number only
(20)

5. Bool - boolean “true/false”


E.g bool( )

Randomisa,on
E.g Int.random(in: lower…upper-bound)

Range Operator
1. Close range (lower…upper)
2. Half-open range(lower..<upper) between lower and upper but not included the
upper bound

Random Element from Array


E.g array.randomElement( )
array.shuffle( ) - change the posi=on randomly inside array

PART III
Func,ons and Scope
⁃ Func8ons func getMilk () {//do stuff //bunch of codes }
example: func gree=ng1() {
print (“Hello”)
}
gree=ng1()
⁃ Scope - code inside the func=on
⁃ Func8ons with inputs func myFunc=on(parameter: DataType) { //do
something to input }
example: func gree=ng2(name: String) {
print (“Hello” \(name))
}
gree=ng2(name: “Angela”)
⁃ Func8ons with output
func funcName (inputName: Data Type) -> Return Type {
let *variable* = inputName - 2
return *variable*
}
example:
func getMilk (money: Int) -> Int {
let change = money - 2
return change
}
Calling the func8on
var change = getMilk(4)
example: func gree=ng3(name: String) -> Bool {
if name == “Angela” || name == “Des=n” {
return true
}
else {
return false
}
}
gree=ng3(name: “Angela”) *output is true*

IF/ELSE
-IF variable = what {
go()
}
ELSE IF VARIABLE ==what{
useJudgment()
}
ELSE {
stop()
}

== is equal to
!= is not equal to
> greater than
< less than
>= greater than or equal to
&& and
|| or
! not

SWITCH STATEMENT
if condi=ons > 5 {
useSwitch()
}
else{
useIfElse()
}

switch value {
case panern: code
case panern: code
default: code
}

ex. func myFunc=on(parameter: DataType) { //do something to input


switch parameter {
case pa1ern: code
case pa1ern: code
default: code }

DICTIONARY
var dict =[“key”: value]
var dict =[String: Int]
let nameData= [“Name”: value, “Name”: value]
RETREIVING ITEM FROM DICTIONARY: dict[“Angela”]

OPTIONALS
var hardness: String?
*the Data type (String) can be empty (op=onal)
1. Force Unwrapping: op=onal!
2. Check for nil value: if op=onal! = nil { safeOp=onal}
3. Op=onal Binding: if let safeOp=onal = op=onal { safeOp=onal}
4. Nil Coalescing Operator: op=onal ?? defaultValue
5. Op=onal Chaining: op=onal?.property op=onal?.method

Structures
-help us create custom data types
-basic data types include String, Array, Bool, Dic8onary etc..
Defining Structure
struct MyStruct {
}
ini8alising the Structure or Calling the Structure
MyStruct()
Examples:

struct Town {
let name = "AngelaLand"
var ci=zens = ["Angela", "Jack Bauer"]
var resources = ["Grain": 100, "Ore": 42, "Wool": 75]

func for=fy() {
print("Defences Increased!")
}
}

var myTown = Town()

print(myTown.ci=zens)
print("\(myTown.name) has \(myTown.resources["Grain"]!) bags of grain")

myTown.ci=zens.append("Keanu Reeves")
print(myTown.ci=zens.count)

myTown.for=fy()

Crea8ng ini8aliser
init() {
}
Example:
struct Town {
let name: String
var ci=zens: [String]
var resources: [String: Int]

init(name: String, ci=zens: [String], resources: [String: Int]){


self.name = name
self.ci=zens = ci=zens
self.resources = resources

}
}

var anotherTown = Town(name: "Nameless Island", ci=zens: ["Tom Hanks"], resources:


["Coconuts": 100])

anotherTown.ci=zens.append("Wilson")

print(anotherTown.ci=zens)

Immutability
-if you are making a method that modifies one of the property inside a structure, then
we to mark that method as muta=ng e.g. muta8ng func fu

Classes
-a way of defining a blueprint like struct, different from struct due to Inheretance

Defining a Class class MyClassName {


//insert proper=es
}

class MyClassName: SuperClass {


//insert proper=es
}

Ini8alise the Class


MyClassName ()
Inheritance
SubClass
SuperClass
override
super.method()

Struct vs Classes
Similari8es
-both create blueprints

Differences
-Structs are Passed by Value, Immutable
-Classes are Passed by References, Inheritance

PROTOCOLS

Defining the Protocol

protocol MyProtocolName {
//Define requirements
}

Adop8ng the Protocol

struct MystructName: MyProtocolName {

CLOSURES
-anonymous func=on, func=on without a name

e.g { (input) -> output in


return outputCode
}

You might also like