Skip to content

Instantly share code, notes, and snippets.

danielgarbien

View GitHub Profile
View KeyboardAwareScrollViewCoordinator.swift
import Foundation
import UIKit
/**
* Coordinates contentInset on scroll view to always show full content on keyboard appearing.
*/
class KeyboardAwareScrollViewCoordinator: NSObject {
let scrollView: UIScrollView
private let originalContentInset: UIEdgeInsets
View FetchedResultsControllerTableDelegate.swift
import Foundation
import UIKit
import CoreData
class FetchedResultsControllerTableDelegate: NSObject {
let tableView: UITableView
var preserveSelection: Bool = true
var didEndUpdates: (() -> Void)?
View CoreDataStack.swift
import Foundation
import CoreData
class CoreDataStack {
let mainContext: NSManagedObjectContext
private let persistentStoreCoordinator: NSPersistentStoreCoordinator
private let managedObjectModel: NSManagedObjectModel
View FetchedResultsTableDataSource.swift
import Foundation
import UIKit
import CoreData
class FetchedResultsTableDataSource: NSObject {
typealias CellForRowAtIndexPath = (tableView: UITableView, indexPath: NSIndexPath, object: AnyObject) -> UITableViewCell
let fetchedResultController: NSFetchedResultsController
private let cellForRowAtIndexPath: CellForRowAtIndexPath
View ContainmentWithAutoLayout.swift
import UIKit
extension UIViewController {
/**
Add childController with no transition.
Embeds its view in container.
Calls didMove(toParentViewController:) on childController at a last step.
*/
func addImmediately(childController: UIViewController, embeddedIn container: UIView) {
View SafeFetchedResultsController.swift
import Foundation
import CoreData
class SafeFetchedResultsController: NSFetchedResultsController {
private weak var safeDelegate: NSFetchedResultsControllerDelegate?
override var delegate: NSFetchedResultsControllerDelegate? {
get {
return safeDelegate
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)
@danielgarbien
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 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]
}
View Delay.swift
func delay(_ delay: Double, closure: @escaping () -> Void) {
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: closure)
}
You can’t perform that action at this time.