Skip to content

Instantly share code, notes, and snippets.

View dineshba's full-sized avatar
👨‍💻
Trying to be an OSS contributor

Dinesh B dineshba

👨‍💻
Trying to be an OSS contributor
View GitHub Profile
extension Double { //swift class
func half() -> Double {
return self / 2
}
}
1.3.half() //0.6500000000000000
1000.0.half() // 500
Double(999).half() // 499.5
if let laptop = programmer.laptop { // set the value of laptop if `programmer.laptop` is not nil and
// enter into if-case or enter into `else` case.
if let laptopFan = laptop.fan { // set the value of laptopFanName if `laptop.fan` is not nil and
// enter into if-case or enter into `else` case.
print("\(laptop.name) contains \(laptopFan.name) fan")
} else {
print("\(laptop.name) contains no fan") // Note: String interpolation "\(value)"
}
} else {
print("\(programmer.name) have no laptop")
func printSummary(_ programmer: Programmer) {
guard let laptop = programmer.laptop else { // set the value of laptop if `programmer.laptop` is not nil
print("\(programmer.name) have no laptop") // and will go line 6. If `programmer.laptop` is nil, it will
return // enter into else case and return to caller of this function.
}
guard let laptopFan = laptop.fan else { // Syntax
print("\(laptop.name) contains no fan") // guard let constant = expression else {
return // return returnValue
} // }
print("\(laptop.name) contains \(laptopFan.name) fan")
let fanNames = computerLab.laptops
.filter({laptop in !laptop.name.contains("Mac") })
.filter({laptop in laptop.fan != nil})
.map({laptop in laptop.fan!.name})
// Simplified
let fanNames = computerLab.laptops
.filter({laptop in !laptop.name.contains("Mac") })
.compactMap({laptop in laptop.fan?.name}) //compactMap will filter out nil values by default
// mandatory argument names for caller
func add(augend: Int, addend: Int) -> Int {
return augend + addend
}
let sum = add(augend: 5, addend: 2)
// no argument names for caller (underscore for no name)
func add(_ augend: Int, _ addend: Int) -> Int {
return augend + addend
}
if case .sugar = sugarcane.taste { // if sugarcane.taste is `.sugar` then
print("yummmmy sugarcane") // enters into if
}
if case .salty(let value) = frenchFries.taste { // frenchFries.taste is .salty(10), so it matches
if value < 10 { // the case and assign the value 10 to `value`
print("manageable") // we can use the `value` inside the `if case`
} else {
print("impossible")
}
}
struct Dog {
var name: String
var weight: Int
}
var myDog = Dog(name: "Ranger", weight: 10)
var yourDog = myDog //Deep copy
yourDog.name = "Puncher"
print(myDog) // Dog(name: "Ranger", weight: 10)
print(yourDog) // Dog(name: "Puncher", weight: 10)
class Dog {
var name: String
var weight: Int
// constructor or initializer
init(name: String, weight: Int) {
self.name = name
self.weight = weight
}
}
enum Taste {
case sweet
case bitter
case sour
case spicy
case salty(Int)
}
struct Cherry: Eatable {
var taste: Taste = Taste.sweet
func isBitter() -> Bool {
return self.taste == .bitter
}
}
let 🍒 = Cherry() // literally we can have variable name like this🤓
struct Chilly: Eatable {
var taste: Taste = Taste.spicy