Skip to content

Instantly share code, notes, and snippets.

View CassiusPacheco's full-sized avatar

Cassius Pacheco CassiusPacheco

View GitHub Profile
@CassiusPacheco
CassiusPacheco / Assert.swift
Created April 29, 2020 06:57
Assert helper that provides a few functions for asserting while debugging
public final class Assert {
public static var shouldCrashOnDebug = true
fileprivate static var isUnitTesting = NSClassFromString("XCTest") != nil
}
public func debugAssertion(_ condition: @autoclosure () -> Bool, message: String = "") {
#if DEBUG || STAGE
if !condition() {
if Assert.shouldCrashOnDebug {
@CassiusPacheco
CassiusPacheco / SingleDisposer.swift
Created April 28, 2020 11:54
A dispose bag that keeps only one disposable in the bag at a time.
import Foundation
import RxSwift
extension Disposable {
/// Adds `self` to `single`
///
/// - parameter single: `SingleDisposer` to add `self` to.
/// - note: `SingleDisposer` is a wrapper of `DisposeBag` that is recreated
/// whenever a disposable object is inserted into it, cancelling any
/// previous subscriptions. It keeps at most one subscription at a
@CassiusPacheco
CassiusPacheco / DependencyContainer.swift
Last active March 17, 2020 10:51
Dependency Container
//
import Foundation
extension NSLocking {
@discardableResult
public func sync<T>(action: () -> T) -> T {
lock()
defer { self.unlock() }
import Foundation
/// This property wrapper offers a locking mechanism for accessing/mutating values in a safer
/// way. Bear in mind that operations such as `+=` or executions of `if let` to read and then
/// mutate values are *unsafe*. Each time you access the variable to read it, it acquires the lock,
/// then once the read is finished it releases it. The following operation is to mutate the value, which
/// requires the lock to be mechanism again, however, another thread may have already claimed the lock
/// in between these two operations and have potentially changed the value. This may cause unexpected
/// results or crashes.
/// In order to ensure you've acquired the lock for a certain amount of time use the `mutate` method.
import UIKit
extension UIViewController {
/// Goes recursively through the `window`'s `rootViewController` hierarchy to return the top most controller.
/// This method takes care of multiple presentation stacks being used by different view controllers, navigation controllers
/// and tab bar controllers.
/// May return `nil` if the window's `rootViewController` is not set. For all the other cases it should return a
/// valid controller as long as there's something visible to the users.
@objc
func topMostViewController(for window: UIWindow? = UIApplication.shared.windows.first) -> UIViewController? {
import Foundation
import RxSwift
// Smart Retry idea originally from: http://kean.github.io/post/smart-retry
public enum DelayOptions {
case immediate
// Time is in milliseconds
case constant(time: Int)
// Initial is in milliseonds
case exponential(initial: Int, multiplier: Double)
//
import Foundation
import UIKit
public func associatedObject<ValueType: Any>(base: AnyObject, key: UnsafePointer<UInt8>, initializer: () -> ValueType)
-> ValueType {
// if there is already an associated value returns it
if let associated = objc_getAssociatedObject(base, key) as? ValueType {
return associated
@CassiusPacheco
CassiusPacheco / TextContentType.swift
Last active October 8, 2019 20:25
(iOS 12 compatible) UITextField extension to handle `textContentType` without dealing with iOS version checking.
//
// TextContentType.swift
// textfield
//
// Created by Cassius Pacheco on 5/6/18.
// Copyright © 2018 Cassius Pacheco. All rights reserved.
//
import UIKit

Keybase proof

I hereby claim:

  • I am cassiuspacheco on github.
  • I am cassiusop (https://keybase.io/cassiusop) on keybase.
  • I have a public key ASBcBLvibntJ902rdW7l5CQsei6PPZZHJvMpNAmqRA91qwo

To claim this, I am signing this object:

@CassiusPacheco
CassiusPacheco / iOS11_TextContentType.swift
Created June 5, 2018 20:36
(iOS 11 compatible) UITextField extension to handle `textContentType` without dealing with iOS version checking.
//
// TextContentType.swift
// textfield
//
// Created by Cassius Pacheco on 5/6/18.
// Copyright © 2018 Cassius Pacheco. All rights reserved.
//
import UIKit