Skip to content

Instantly share code, notes, and snippets.

@nanoxd
nanoxd / KeyPath+Closures.swift
Last active December 10, 2018 12:20
Key Path for Closures
prefix operator ^
prefix func ^<Root, Value>(keyPath: KeyPath<Root, Value>) -> (Root) -> Value {
return { $0[keyPath: keyPath] }
}
@nanoxd
nanoxd / Optional+KeyPathMap.swift
Last active September 22, 2018 16:46
[Optional+KeyPathMap] Maps an optional value to a property on the object using a keyPath. #swift
extension Optional {
public func map<T>(_ keyPath: KeyPath<Wrapped, T>) -> T? {
return map({ $0[keyPath: keyPath] })
}
}
@nanoxd
nanoxd / UIImage+RenderMode.swift
Last active September 22, 2018 16:45
[UIImage+RenderMode] Easily use a different rendering mode on an UIImage #uikit #swift
public extension UIImage {
public var original: UIImage { return withRenderingMode(.alwaysOriginal) }
public var template: UIImage { return withRenderingMode(.alwaysTemplate) }
}
@nanoxd
nanoxd / ScreenShottable.swift
Last active September 22, 2018 16:45
[ScreenShottable] Screenshot a view using UIGraphicsImageRenderer #swift #uikit
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)
@nanoxd
nanoxd / UIEdgeInsets+ExpressibleByDictionaryLiteral.swift
Last active September 22, 2018 16:45
[UIEdgeInsets+DictionaryLiteral] Use a dictionary literal for UIEdgeInsets #swift #uikit
extension UIEdgeInsets: ExpressibleByDictionaryLiteral {
public typealias Key = EdgeKey
public typealias Value = CGFloat
public enum EdgeKey {
case top
case left
case bottom
case right
}
@nanoxd
nanoxd / Equatable+IsAny.swift
Last active September 22, 2018 16:44
[Equatable+isAny] Check if an item that is equatable contains any of the items #swift
extension Equatable {
func isAny(of candidates: Self...) -> Bool {
return candidates.contains(self)
}
}
// Usage:
[1, 2, 3].isAny(of: 4, 1) // => true
@nanoxd
nanoxd / UIInsetLabel.swift
Last active September 22, 2018 16:43
[UIInsetLabel] A custom UILabel that can be inset #swift #uikit
/// 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)
}
@nanoxd
nanoxd / CustomNamespace.swift
Last active October 6, 2018 16:20
[Custom Namespace] An example of how to create a custom namespace for your app to avoid clobbering existing names #swift
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 {
@nanoxd
nanoxd / OptionalType+Sugar.swift
Last active December 30, 2018 11:13
[Optional+Sugar] Adds nice ways to interact with optionals #swift
/// Type erased Optional protocol
protocol OptionalType {
associatedtype WrapType
var isSome: Bool { get }
var isNone: Bool { get }
var unsafelyUnwrapped: WrapType { get }
}
@nanoxd
nanoxd / ObservableType+.swift
Last active December 16, 2018 02:36
[ObservableType+] Adds additional sugar to `ObservableType`
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! }