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
enum Dependencies { | |
// ... | |
final class Container { | |
// ... | |
func register(_ dependency: Any, for key: Dependencies.Name = .default) { | |
dependencies.append((key: key, value: dependency)) | |
} | |
func resolve<T>(_ key: Dependencies.Name = .default) -> T { | |
return (dependencies |
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
// We will create an enum as a namespace | |
enum Dependencies { | |
// The same pattern that NotificationCenter's Name struct | |
// follows. | |
struct Name: Equatable { | |
let rawValue: String | |
static let `default` = Name(rawValue: "__default__") | |
static func == (lhs: Name, rhs: Name) -> Bool { lhs.rawValue == rhs.rawValue } | |
} |
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
struct Person { | |
let name: String | |
} | |
struct People { | |
// This empty array will be the wrappedValue in the init. | |
@Sorted(by: { $0.name < $1.name }) var items: [Person] = [] | |
} | |
var people = People() |
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
// @propertyWrapper annotation makes this | |
// struct a property wrapper. | |
// It will be a generic struct because we will make | |
// it work with any kind of value type. | |
@propertyWrapper | |
struct Sorted<T> { | |
// Internally, we will store the actual value of | |
// the array in this property | |
private var value: [T] |
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 ProductsCalculator { | |
// ... | |
func calculateTotal(using taxesCalculator: TaxesCalculatorType) -> Double { | |
let subtotal = products.map({ $0.price }).reduce(0, +) | |
return taxesCalculator.applyTaxes(to: subtotal) | |
} | |
} |
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 ProductsCalculator { | |
private let taxesCalculator: TaxesCalculatorType | |
// ... | |
init(taxesCalculator: TaxesCalculatorType) { | |
self.taxesCalculator = taxesCalculator | |
} | |
func calculateTotal() -> Double { |
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
protocol TaxesCalculatorType { | |
func applyTaxes(to subtotal: Double) -> Double | |
} | |
class TaxesCalculatorA: TaxesCalculatorType { | |
func applyTaxes(to subtotal: Double) -> Double { | |
// Some weird taxes logic 😨 | |
subtotal * 1.21 | |
} | |
} |
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 TaxesCalculator { | |
func applyTaxes(to subtotal: Double) -> Double { | |
// Some weird taxes logic 😨 | |
subtotal * 1.21 | |
} | |
} | |
class ProductsCalculator { | |
private let taxesCalculator = TaxesCalculator() |
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
struct Product { | |
// ... | |
let price: Double | |
} | |
class ProductsCalculator { | |
private var products: [Product] = [] | |
func add(product: Product) { | |
products.append(product) |
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
final class ViewController: UIViewController { | |
// MARK: - Views - | |
@IBOutlet private weak var firstNumberTextField: UITextField! | |
@IBOutlet private weak var secondNumberTextField: UITextField! | |
@IBOutlet private weak var operationTextField: UITextField! | |
@IBOutlet private weak var resultLabel: UILabel! | |
private var operationPickerView: UIPickerView! | |