Skip to content

Instantly share code, notes, and snippets.

View String+UIImage.swift
extension String {
func draw(fontSize: CGFloat) -> UIImage? {
draw(font: .systemFont(ofSize: fontSize))
func draw(font: UIFont) -> UIImage? {
draw(attributes: [.font: font], backgroundColor: .clear)
View Codable+NSCoding.swift
extension KeyedDecodingContainer {
func decode<T: NSCoding>(_ type: T.Type, forKey key: KeyedDecodingContainer<K>.Key) throws -> T? {
let data = try decode(Data.self, forKey: key)
return try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? T ?? nil
extension KeyedEncodingContainer {
View Timer_iOS9
private class BlockHandler: NSObject {
var block: (() -> Void)?
@objc func runBlock() {
View FullContentIntrinsicSizeTableView.swift
import UIKit
class FullContentIntrinsicSizeTableView: UITableView {
override var contentSize: CGSize {
didSet {
if contentSize != oldValue {
View UIAlertController+LeftAlignedMessage.swift
import UIKit
extension UIAlertController {
func setLeftAlignedMessage(_ message: String) {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .left
let messageText = NSMutableAttributedString(
string: message,
View UITableView+Registration
import UIKit
extension UITableView {
func dequeueReusableCell(withIdentifier identifier: String, registerNibIfNeededWithNibName nibName: String) -> UITableViewCell? {
guard let cell = dequeueReusableCell(withIdentifier: identifier) else {
register(UINib(nibName: nibName, bundle: nil), forCellReuseIdentifier: identifier)
return dequeueReusableCell(withIdentifier: identifier)
View Delay.swift
func delay(_ delay: Double, closure: @escaping () -> Void) {
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: closure)
View SimpleCollectionViewDataSource.swift
import Foundation
import UIKit
class SimpleCollectionViewDataSource<Cell: AnyObject, Object>: NSObject, UICollectionViewDataSource {
var objects: [[Object]]
func objectAtIndexPath(indexPath: NSIndexPath) -> Object {
return objects[indexPath.section][indexPath.row]
danielgarbien / SessionDelegate.swift
Last active Mar 14, 2018
Simple network synchronizer with caching
View SessionDelegate.swift
import Foundation
The only benefit SessionDelegate provides is to accompany URL respones with cache control headers which are not provided by the backend at the time of writing.
(Cache control headers in responses let us use Foundation's NSURLCache abilities.)
Once the backend implementation is changed to provide responses with cache control headers SessionDelegate should no longer be used.
NOT THREAD SAFE - it should be used on the delegation queue of NSURLSession
View NDJSONParserTests.swift
import Foundation
import XCTest
class NDJSONParserTests: XCTestCase {
let ndJson = "{ \"key\": \"value\" }\n"
func testSimpleNDJSON() {
let twoLines = ndJson + ndJson
XCTAssert(parseDataFromString(twoLines).count == 2)