Skip to content

Instantly share code, notes, and snippets.

@Gabbrolee
Last active August 8, 2021 09:50
Show Gist options
  • Save Gabbrolee/f9e36a599df897eb508fd43ae4119142 to your computer and use it in GitHub Desktop.
Save Gabbrolee/f9e36a599df897eb508fd43ae4119142 to your computer and use it in GitHub Desktop.
dayThree.swift
import UIKit
// classes, struct, optional, optional chaining, enum
// this function return string but what if the
// haters don't feel like hating today then it
// is optional
// in a layman understanding, it means either one
// has respond to provide to questions or not
// the ? makes it optional
func getHaterStatus(weather:String) -> String? {
if weather == "sunny" {
return nil
}else {
return "Hate"
}
}
var status = getHaterStatus(weather: "rainy")
// optional unwrapping: is done in a condition statement
// it check if an optional has a value and
if let unwrappedStatus = status {
// unwrappedStatus contains a non-optonal string
}else {
// in case you want an else block, here you go...
}
func takeHaterAction(status: String){
if status == "Hate" {
print("Hating")
}
}
if let haterStatus = getHaterStatus(weather: "rainy"){
takeHaterAction(status: haterStatus)
}
// if we have a function yearAlbumRelease
// with the following condition and return value
// what if the yearAbumRelease was not found
//
//func yearAlbumRelease(name: String) -> Int? {
// if name == "Taylor Swift" {return 2006}
// if name == "Fearless" {return 2008}
//if none of the year fail to be return then 0 will
//be return but then 0 is not that bad only that no //album of such could have been release in 0AD
//because of that I will comment out zero and //replace it with nil and it shows that if //the name of the album is not found,
//it should return nil
//return nil
var items = ["James", "John", "Sally"]
func position(of string: String, in array: [String]) -> Int {
for i in 0 ..< array.count {
if array[i] == string {
return i
}
}
return 0
}
let jamesPosition = position(of: "James", in: items)
let johnPosition = position(of: "John", in: items)
let bobPosition = position(of: "Bob", in: items)
let sallyPosition = position(of: "Sally", in: items)
// it is obvious that Bob is not in the array
// Bob is having index 0
// the solution to this is optionals
// nil should be return when the position is
// not found
// Better implementation of the yearAlbumReleased
func yearAlbumReleased(name: String) -> Int? {
if name == "Taylor Swift" {return 2006}
if name == "Fearless" {return 2008}
return nil
}
var year = yearAlbumReleased(name: "Taylor Swift")
if year == nil {
print("There was an error")
}else {
print ("It was released in \(year!)") // optional 2006
}
// to remove the optional 2006, we have
//to force unwrap it since we are sure
//there is a value by adding ! to the year
var name: String = "Paul"
var name2: String? = "Bob"
var name3: String! = "Sophie"
//implicitly unwrapping optional
//MARKS:- OPTIONAL CHAINING
// this help one code only if one optional has a value
func albumReleased1(year: Int) -> String? {
switch year {
case 2006: return "Taylor Swift"
case 2008: return "Taylor Swift"
default:
return nil
}
}
// this is where optional chaining is
// when it return a value it should make it
// uppercase else if no value then nil
// should be return
let album = albumReleased1(year: 2006)?.uppercased()
print("The album is \(album)")
let str = "hello world"
print(str.uppercased())
//nil coalescing operator helps one check
// value in right code then left operator
// if the album is not found return nil
// if the album is found print it
let album1 = albumReleased1(year: 2006) ?? "unknown"
print("The album is \(album1)")
//MARKS:-ENUMS
enum WeatherType {
case sun
case cloud
case rain
case wind
case snow
}
func getHaterStatus1(weather: WeatherType) -> String? {
if weather == .sun {
return nil
} else {
return "Hate"
}
}
getHaterStatus1(weather: .sun)
// switch case can also be use for the above
// WeatherType.sun can be modify to .sun instead
enum weatherType {
case sun
case cloud
case rain
case wind(speed: Int)
case snow
}
//func getHateStatus4(weather: WeatherType) -> String? {
// switch weather {
// case .sun:
// return nil
// case .wind(let speed) where speed < 10:
// return "meh"
// case .cloud, .wind:
// return "dislike"
// case .rain, .snow:
// return "hate"
// }
//}
//MARKS:- STRUCT
// they are complex datatype that is they are made
// up of values
// struct automatically generate a memberwise initializer
//
struct Person {
var clothes: String
var shoes: String
}
let taylor = Person(clothes: "T-shirts", shoes: "sneakers")
let other = Person(clothes: "short skirt", shoes: "high heels")
print(taylor.clothes)
print(other.shoes)
var taylorCopy = taylor
taylorCopy.shoes = "flip flops"
print(taylor)
print(taylorCopy)
//when we add function inside struct we call it method
//MARKS:- CLASS : instance of a class is called object
// a class can be a base of another class
class Person1 {
var clothes: String
var shoes: String
init (clothes: String, shoes: String){
self.clothes = clothes
self.shoes = shoes
}
}
class Singer {
var name: String
var age: Int
init(name: String, age: Int){
self.name = name
self.age = age
}
func sing() {
print("La la la la")
}
}
class CountrySinger: Singer {
override func sing() {
print("Trucks, guitars, and liquor")
}
}
var taylor1 = CountrySinger(name: "Taylor", age: 25)
class HeavyMetalSinger: Singer {
var noiseLevel: Int
init(name: String, age: Int, noiseLevel: Int){
self.noiseLevel = noiseLevel
super.init(name: name, age: age)
}
override func sing() {
print("what's up")
}
}
taylor1 = CountrySinger(name: "Taylor", age: 25)
//where taylor1 shows instantiation of class CountrySinger
//where HeavyMetalSinger: Singer shows that
// HeavyMetalSinger is a child class of the
// parent class
// the override is use to set a method for
// the child class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment