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
| public extension UIView { | |
| func removeAllSubviews() { | |
| let subviews = self.subviews | |
| subviews.forEach { | |
| $0.removeFromSuperview() | |
| } | |
| } | |
| } |
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
| public extension UIView { | |
| func forceLayout() { | |
| setNeedsLayout() | |
| layoutIfNeeded() | |
| } | |
| } |
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
| public extension DateFormatter { | |
| convenience init(dateFormat: String, locale: Locale) { | |
| self.init() | |
| self.dateFormat = dateFormat | |
| self.locale = locale | |
| } | |
| } |
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
| public extension Int { | |
| var asString: String { | |
| "\(self)" | |
| } | |
| func toString() -> String { | |
| "\(self)" | |
| } | |
| } |
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 | |
| protocol User: Identifiable { | |
| var firstName: String { get set } | |
| var lastName: String { get set } | |
| } | |
| func printUserName(_ user: any User) { | |
| print("\(user.firstName) \(user.lastName)") | |
| } |
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 | |
| struct Item { | |
| var name: String | |
| var price: Int | |
| } | |
| extension Array { | |
| func map<Value>(_ keyPath: KeyPath<Element, Value>) -> [Value] { | |
| map { $0[keyPath: keyPath] } |
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 | |
| protocol Hero { | |
| static func weapon() -> String | |
| static func fight() | |
| } | |
| extension Hero { | |
| static func fight() { | |
| print("Fighting with... \(weapon())") |
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
| let point1 = (1.5, 0.5) | |
| let point2 = (1.0, 0.0) | |
| let point3 = (0.5, 0.5) | |
| for point in [point1, point2, point3] { | |
| switch point { | |
| case let (x, y) where pow(x, 2) + pow(y, 2) < 1.0: | |
| print("Point \(x), \(y) is inside the circle") | |
| case let (x, y) where pow(x, 2) + pow(y, 2) == 1.0: |
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 UIKit | |
| extension UIViewController { | |
| @objc func altViewDidAppear(_ animated: Bool) { | |
| print("View did appear - \(type(of: self))") | |
| altViewDidAppear(animated) | |
| } | |
| static func swizzle() { | |
| let originalSelector = #selector(UIViewController.viewDidAppear(_:)) |
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
| protocol PItemCollection { | |
| associatedtype ItemType | |
| var items: [ItemType] { get set } | |
| mutating func add(item: ItemType) | |
| mutating func clear() | |
| } | |
| extension PItemCollection { | |
| mutating func add(item: ItemType) { |