Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / UIButton+Rx.swift
Last active September 22, 2018 16:47
[UIButton+IsCheckedRx] Adds checked behavior to UIButton #uikit #swift
extension Reactive where Base: UIButton {
/// Alternated with button taps to provide
/// a checkbox behavior by `UIButton`
var checked: ControlProperty<Bool> {
let button = self.base
let source: Observable<Bool> = self.tap
.map {[weak button] in
guard let state = button?.isSelected else {
return false
}
@nanoxd
nanoxd / run.sh
Created September 27, 2018 10:52
[Debugging iOS] Useful ways to check your binary
# Display shared libraries that your binary attempts to load
otool -L <binary>
# Display DYLD symbols for exports and imports
nm <binary>
@nanoxd
nanoxd / playground.swift
Created October 1, 2018 13:25
[XCTest in Playgrounds] Use XCTest in Xcode Playground
import XCTest
class MyTestCase: XCTestCase {
func testExample() {
XCTAssertTrue(true)
}
}
MyTestCase.defaultTestSuite.run()
@nanoxd
nanoxd / MulticastDelegate.swift
Created October 5, 2018 00:20
[MulticastDelegate] #swift Utility Class that can hold on to any number of delegates and invoke arbitrary blocks of code on them
/// Utility Class that can hold on to any number of delegates and invoke arbitrary blocks of code on them
public class MulticastDelegate<T: AnyObject> {
/// HashTable containing any number of delegates, held weakly
private let delegates: NSHashTable<AnyObject> = NSHashTable.weakObjects()
/// Adds delegate to list of delegates
///
/// - Parameter delegate: Delegate to add to list of known delegates
public func add(_ delegate: T) {
delegates.add(delegate as AnyObject)
@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 {