Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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! }
@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 / 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 / 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 / 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)