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
import UIKit | |
import AVFoundation | |
class ResizingImageView: UIImageView { | |
private var heightConstraint: NSLayoutConstraint? | |
override var image: UIImage? { | |
didSet { | |
_ = heightConstraint.map { NSLayoutConstraint.deactivate([$0]) } |
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 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 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 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 | |
private class Associated<Type>: NSObject { | |
let value: Type | |
init(_ value: Type) { | |
self.value = value | |
} | |
} | |
protocol Associable {} |
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 Constrainable {} | |
extension Constrainable where Self: UIView { | |
@discardableResult | |
func constrain(constraints: (Self) -> [NSLayoutConstraint]) -> [NSLayoutConstraint] { | |
let constraints = constraints(self) | |
self.translatesAutoresizingMaskIntoConstraints = false | |
NSLayoutConstraint.activate(constraints) | |
return constraints |
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
import UIKit | |
class NotificationManager { | |
private var tokens = [String: AnyObject]() | |
deinit { | |
tokens.values.forEach { NC.removeObserver($0) } | |
} | |
func addObserver(forName name: NSNotification.Name, object obj: Any?, queue: OperationQueue?, using block: @escaping (Notification) -> Void) { |
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
let url = URL() | |
_ = UIImageView.af_sharedImageDownloader.imageCache?.removeImage(for: URLRequest(url: url), withIdentifier: nil) | |
imageView.af_setImage(withURL: url) |
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
import Foundation | |
typealias Points = Int | |
extension Points { | |
func rank() -> Rank { | |
return Rank(self) | |
} | |
} |
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
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 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 | |
} |
OlderNewer