Skip to content

Instantly share code, notes, and snippets.

View DanielCardonaRojas's full-sized avatar

Daniel Cardona Rojas DanielCardonaRojas

View GitHub Profile
extension String {
func isContainedBy(set: CharacterSet) -> Bool {
let letters = self.components(separatedBy: set)
print(letters)
return self.count == letters.count
}
}
@DanielCardonaRojas
DanielCardonaRojas / MiscExtensions.swift
Created November 8, 2018 14:16
Random Swift Extensions (TODO: Order as this gist grows)
extension UIEdgeInsets {
static func with(_ constant: CGFloat) -> UIEdgeInsets {
return UIEdgeInsets(top: constant, left: constant, bottom: constant, right: constant)
}
}
extension Comparable {
func clamp(min: Self, max: Self) -> Self {
if self < min {
return min
@DanielCardonaRojas
DanielCardonaRojas / UserDefaultsExtension.swift
Created January 28, 2019 22:12
Serialize/deserialize any Codable type to/from UserDefaults
extension UserDefaults {
@discardableResult
func serialize<V: Codable>(_ value: V, forKey key: String) -> Bool {
if let data = value.asPlistData {
self.set(data, forKey: key)
return self.synchronize()
}
return false
@DanielCardonaRojas
DanielCardonaRojas / TextFormatter.swift
Last active March 8, 2019 14:01
A class simplifies handling NSAttributedString properties.
//
// TextViewFormatter.swift
// TextViewFormatter
//
// Created by Daniel Esteban Cardona Rojas on 3/6/19.
// Copyright © 2019 Daniel Esteban Cardona Rojas. All rights reserved.
//
import UIKit
/*
extension UITextView {
var currentWord: String? {
return currentWordRange.map({ String(text[$0]) })
}
var currentWordRange: Range<String.Index>? {
let regex = try! NSRegularExpression(pattern: "\\S+$")
let textRange = NSRange(location: 0, length: selectedRange.location)
if let range = regex.firstMatch(in: text, range: textRange)?.range {
return Range(range, in: text)
@DanielCardonaRojas
DanielCardonaRojas / BasicSwiftProtocolExtensions.swift
Last active April 3, 2019 14:32
Extensions on Equatable, Comparable, etc..
extension Comparable {
func clamped(min: Self, max: Self) -> Self {
if self < min {
return min
}
if self > max {
return max
}
@DanielCardonaRojas
DanielCardonaRojas / Coordinator.swift
Created April 13, 2019 13:29
Base protocol for adopting Coordinator pattern.
import UIKit
import Foundation
protocol Coordinator: class {
typealias ExitHandler = (Coordinator) -> Void
var childCoordinators: [Coordinator] { get set }
var navigationController: UINavigationController { get set }
var initialViewController: UIViewController? { get }
func willNavigate(from: UIViewController, to: UIViewController)
func start()
@DanielCardonaRojas
DanielCardonaRojas / Storyboarded.swift
Created April 13, 2019 14:04
Helper protocol for easy controller instantiation.
/*
It is encouraged to use the same class name for the controller story board identifier,
but this protocol is flexible to allow defining the story from where to instantiate the
controller as well its identifier.
You can choose to keep the convention while just changing the story board where the controller
is located.
e.g:
@DanielCardonaRojas
DanielCardonaRojas / RevealView.swift
Created April 15, 2019 13:15
Collapsing expanding view
@IBDesignable
class RevealView: UIView {
var contentHeight: CGFloat = 300
@IBInspectable var title: String = "" {
didSet {
titleLabel.text = title
}
}
@DanielCardonaRojas
DanielCardonaRojas / Cached.swift
Created May 30, 2019 14:56
Cached computed properties
struct Cached <V, C> {
typealias Computation = (V) -> C
private var computation: (V) -> C
private var cachedResult: C?
var value: V {
willSet {
cachedResult = nil
}
}