Last active
March 31, 2021 15:41
-
-
Save OyegokeTomisin/93c404202e5eac08e2d2db38143ded24 to your computer and use it in GitHub Desktop.
Tuesday's QnA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| // MARK:- Extensions | |
| let tomiNumber: String = "08080008009" | |
| let car = "toyota" | |
| extension String { | |
| func addAreaCode() -> String { | |
| return "+234" + self | |
| } | |
| } | |
| tomiNumber.addAreaCode() | |
| car.addAreaCode() | |
| // MARK:- Structs VS Classes | |
| final class Humans { | |
| var name: String | |
| init(name: String) { | |
| self.name = name | |
| } | |
| } | |
| extension Humans { | |
| func eat() -> String { | |
| return "\(name) is eating" | |
| } | |
| } | |
| class Person { | |
| var name: String | |
| init(name: String) { | |
| self.name = name | |
| } | |
| } | |
| struct AnotherPerson { | |
| var name: String | |
| } | |
| // Adress system is called a pointer | |
| let personA = Person(name: "James") // XYZ | |
| let personB = personA // XYZ | |
| let personC = personB // XYZ | |
| personA.name | |
| personB.name | |
| personC.name | |
| personC.name = "James Bond" | |
| personA.name | |
| personB.name | |
| personC.name | |
| let personD = Person(name: "Tomi") // LMN | |
| let personE = personD // LMN | |
| var another = AnotherPerson(name: "Luke") // ABC | |
| var anotherB = another // New address | |
| print(another.name) | |
| print(anotherB.name) | |
| anotherB.name = "Luke Skywalker" | |
| print(another.name) | |
| print(anotherB.name) | |
| // MARK:- Type Casting | |
| var arrayOfStuff: [Any] = ["tomi", 3, 4.0] | |
| // Type cast - Force or Make a type to take up alternate behaviour | |
| let number = arrayOfStuff[1] // Any | |
| if let anyNumberAsInteger = number as? Int { // Cast from Any to Int { | |
| let convertedInteger = String(anyNumberAsInteger) | |
| print(convertedInteger) | |
| } | |
| if let anyNumberAsString = arrayOfStuff[0] as? String { // Cast from Any to String { | |
| print(anyNumberAsString) | |
| } | |
| // MARK:- Loops and String Interpolation | |
| // String is collection | |
| let greetingArray = ["how", "are", "you", "?"] // how are you ? | |
| var greeting = "" | |
| // For each | |
| // Map or Compact map | |
| for value in greetingArray { | |
| greeting = greeting + value | |
| greeting = greeting + " " | |
| } | |
| print(greeting) | |
| // MARK:- Getters and Setters | |
| class Circle { | |
| var diameter: Int | |
| var radius: Int { | |
| get { return calculateRadius() } // --- Fourth | |
| set { diameter = newValue * 2 } // --- First | |
| // willSet { print("I just calulated area of this shape to be \(newValue)") } // Second | |
| // didSet { print("I just calulated area of this shape") } // Third | |
| } | |
| init(diameter: Int) { | |
| self.diameter = diameter | |
| } | |
| func calculateRadius() -> Int { | |
| return diameter/2 | |
| } | |
| } | |
| var shape = Circle(diameter: 40) | |
| shape.radius = 100 | |
| print(shape.diameter) | |
| // MARK:- Optionals | |
| // String?, Int?, Double?, Bool? | |
| // Unwrapping - Safely use those data types | |
| // if let | |
| // guard let | |
| // Nil coalescing | |
| let name: String? = nil | |
| if let unwrappedName = name { | |
| print(unwrappedName) | |
| } else { | |
| print("do something if name is nil i.e if it does not have a value") | |
| } | |
| func addition() { | |
| guard let unwrappedName = name else { | |
| print("function has exited") | |
| return | |
| } | |
| print(unwrappedName) | |
| } | |
| addition() | |
| print(name ?? "Unidentified") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment