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
prefix operator ^ | |
prefix func ^<Root, Value>(keyPath: KeyPath<Root, Value>) -> (Root) -> Value { | |
return { $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
extension Optional { | |
public func map<T>(_ keyPath: KeyPath<Wrapped, T>) -> T? { | |
return 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
public extension UIImage { | |
public var original: UIImage { return withRenderingMode(.alwaysOriginal) } | |
public var template: UIImage { return withRenderingMode(.alwaysTemplate) } | |
} |
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 Screenshottable { | |
func screenshot() -> UIImage? | |
} | |
extension UIView: Screenshottable { | |
func screenshot() -> UIImage? { | |
let renderer = UIGraphicsImageRenderer(bounds: bounds) | |
return renderer.image { _ in | |
drawHierarchy(in: self.bounds, afterScreenUpdates: false) |
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 UIEdgeInsets: ExpressibleByDictionaryLiteral { | |
public typealias Key = EdgeKey | |
public typealias Value = CGFloat | |
public enum EdgeKey { | |
case top | |
case left | |
case bottom | |
case right | |
} |
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 Equatable { | |
func isAny(of candidates: Self...) -> Bool { | |
return candidates.contains(self) | |
} | |
} | |
// Usage: | |
[1, 2, 3].isAny(of: 4, 1) // => true |
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
/// An UILabel that allows insetting the label | |
class UIInsetLabel: UILabel { | |
/// The insets to inset the main `rect` by | |
var insets: UIEdgeInsets | |
init(insets: UIEdgeInsets) { | |
self.insets = insets | |
super.init(frame: .zero) | |
} | |
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 | |
/** | |
A custom namespace _へ__(‾◡◝ )>. | |
General pattern would be: | |
``` | |
extension CustomNamespace where Base: UIView { | |
// This property does not override the existing one | |
var safeAreaInsets: UIEdgeInsets { |
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
/// Type erased Optional protocol | |
protocol OptionalType { | |
associatedtype WrapType | |
var isSome: Bool { get } | |
var isNone: Bool { get } | |
var unsafelyUnwrapped: WrapType { get } | |
} |
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 | |
import RxSwift | |
import RxCocoa | |
extension ObservableType { | |
/// Unwraps an Optional Element if it's present | |
/// | |
/// - Returns: The safely unwrapped item | |
func unwrap<T>() -> Observable<T> where E == Optional<T> { | |
return filter { $0 != nil }.map { $0! } |