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 Storable {} | |
extension Storable { | |
var storedValue: Any? { | |
get { return UserDefaults.standard["value"] } | |
nonmutating set { UserDefaults.standard["value"] = newValue } | |
} | |
} | |
struct Storage: Storable {} | |
let storage = Storage() | |
storage.storedValue = "🐼" |
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
extension Array { | |
subscript(safe index: Int, repeated repeated: Bool) -> Element? { | |
return isEmpty ? nil : self[index % count] | |
} | |
} |
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 Instantiatable {} | |
extension Instantiatable where Self: UIViewController { | |
static func instantiate() -> Self { | |
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: String(describing: self)) as! Self | |
} | |
} | |
extension UIViewController: Instantiatable {} |
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
enum AppStoryboard: String { | |
case Main | |
var instance: UIStoryboard { | |
return UIStoryboard(name: rawValue, bundle: Bundle.main) | |
} | |
func viewController<T: UIViewController>(of type: T.Type) -> T { | |
return instance.instantiateViewController(withIdentifier: type.storyboardId) as! T | |
} |
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 | |
import AVFoundation | |
class ResizingImageView: UIImageView { | |
private var heightConstraint: NSLayoutConstraint? | |
override var image: UIImage? { | |
didSet { | |
_ = heightConstraint.map { NSLayoutConstraint.deactivate([$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
class Messenger { | |
func log(_ message: String) { | |
print(message) | |
} | |
func logThis() { | |
DispatchQueue.main.async { [weak self] in | |
guard let this = self else { return } | |
this.log("1") |
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 | |
class InsetLabel: UILabel { | |
var insets = UIEdgeInsets() | |
override var intrinsicContentSize: CGSize { | |
var size = super.intrinsicContentSize | |
size.width += insets.left + insets.right | |
size.height += insets.top + insets.bottom |
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
private let swizzling: (AnyClass, Selector, Selector) -> () = { forClass, originalSelector, swizzledSelector in | |
let originalMethod = class_getInstanceMethod(forClass, originalSelector) | |
let swizzledMethod = class_getInstanceMethod(forClass, swizzledSelector) | |
method_exchangeImplementations(originalMethod, swizzledMethod) | |
} | |
extension UIView { | |
open override class func initialize() { | |
// make sure this isn't a subclass |
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 | |
typealias Points = Int | |
extension Points { | |
func rank() -> Rank { | |
return Rank(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
protocol SomeProtocol { | |
associatedtype T | |
typealias Closure = (T) -> Void | |
var blocks: [Closure] { get set } | |
} | |
class SomeClass<T>: SomeProtocol { | |
typealias Closure = (T) -> Void | |
var blocks: [Closure] | |