This file contains 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
//Declare class | |
class Cat { | |
var name: String | |
init(name: String) { | |
self.name = name | |
} | |
} | |
class Dog { | |
var name:String |
This file contains 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
extension Cat: Pet {} | |
extension Dog: Pet {} | |
let pets: [Pet] = [Cat(name: "Mistten"), Dog(name: "Yeeker")] |
This file contains 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
func herd<Animal: Cat> (_ cats: [Animal]) { | |
cats.forEach { | |
print("Here,cat, \($0.name)! come here") | |
} | |
} | |
func herd<Animal: Dog> (_ dogs: [Animal]) { | |
dogs.forEach { | |
print("Here, dog, \($0.name)! come here") | |
} | |
} |
This file contains 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
class Handler { | |
func handle() { | |
let data = requestDataToAPI() | |
let array = parse(data: data) | |
saveToDB(array:array) | |
} | |
private func requestDataToAPI() -> Data { | |
//send API request and wait the response | |
} |
This file contains 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
class Handler { | |
let apiHandler: APIHandler | |
let parseHandler: ParseHandler | |
let dbHandler: DBHandler | |
init(apiHandler: APIHandler, parseHandler:parseHandler, dbHandler:DBHandler) { | |
self.apiHandler = apiHandler | |
self.parseHandler = parseHandler | |
self.dbHandler = dbHandler | |
} |
This file contains 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
class Logger { | |
func printData() { | |
let cars = [ | |
Car(name: "BWM", color: "Green"), | |
Car(name: "Toyota", color: "Blue"), | |
Car(name: "Honda", color: "Black") | |
] | |
cars.forEach { cars in | |
print(car.printDetails()) |
This file contains 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
class Logger { | |
func printData() { | |
let cars = [ | |
Car(name: "BWM", color: "Green"), | |
Car(name: "Toyota", color: "Blue"), | |
Car(name: "Honda", color: "Black") | |
] | |
cars.forEach {cars in | |
print(cars.printDetails()) |
This file contains 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
//SOLID - O | |
protocol Printable { | |
func printDetails() -> String | |
} | |
class Logger { | |
func printData() { | |
let vehices: [Printable] = [ | |
Car(name: "BWM", color: "Green"), | |
Car(name: "Toyota", color: "Blue"), | |
Car(name: "Honda", color: "Black"), |
This file contains 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
//Not preferred: | |
class MyViewController: UIViewController, UITableViewDataSource, UIScrollViewDelegate { | |
// all methods | |
} | |
//Preferred: | |
class MyViewController: UIViewController { | |
// class stuff here | |
} | |
// MARK: - UITableViewDataSource |
This file contains 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
//Preferred: | |
if user.isHappy { | |
// Do something | |
} else { | |
// Do something else | |
} | |
//Not Preferred: | |
if user.isHappy | |
{ |