Skip to content

Instantly share code, notes, and snippets.

View chrisbrandow's full-sized avatar

Chris Brandow chrisbrandow

View GitHub Profile
@chrisbrandow
chrisbrandow / centeredCollectionViewCells
Last active May 17, 2019 03:48
CollectionView with centered cells
/// This class centers the elements that fit on a given row/column
/// ripped off from https://github.com/Coeur/CollectionViewCenteredFlowLayout
/// modified to simplify and to remove forced unwrapping
open class CollectionViewCenteredFlowLayout: UICollectionViewFlowLayout {
open override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
guard let elementsAttributes = super.layoutAttributesForElements(in: rect)
else { return nil }
guard let collectionView = collectionView
else { return elementsAttributes }
@chrisbrandow
chrisbrandow / autolayoutDSL
Created March 26, 2018 19:01
Eidhof's Autolayout DSL with a couple additions
typealias Constraint = (_ child: UIView, _ parent: UIView) -> NSLayoutConstraint
func equal<Axis, Anchor>(_ keyPath: KeyPath<UIView, Anchor>, _ to: KeyPath<UIView, Anchor>, _ constant: CGFloat = 0) -> Constraint where Anchor: NSLayoutAnchor<Axis> {
return { $0[keyPath: keyPath].constraint(equalTo: $1[keyPath: to], constant: constant) }
}
func equal<Axis, Anchor>(_ keyPath: KeyPath<UIView, Anchor>, _ constant: CGFloat = 0) -> Constraint where Anchor: NSLayoutAnchor<Axis> {
return equal(keyPath, keyPath, constant)
}
@chrisbrandow
chrisbrandow / lockSpeedTests.m
Last active December 1, 2017 19:02
Objective-C dispatch Queue Speed Tests
- (void)testQueue {
dispatch_queue_t lockQueue = dispatch_queue_create("com.test.LockQueue", DISPATCH_QUEUE_SERIAL);
[self executeLockTest:^(VoidBlock block) {
dispatch_sync(lockQueue, ^{
block();
});
}];
}
- (void)disabled_testNoLock {
@chrisbrandow
chrisbrandow / FileParse.swift
Created September 1, 2017 19:57
benchmarking String vs. Data reading and processing.
func strings(from name: String) -> [String]? {
let date = Date()
guard let path = Bundle.main.path(forResource: name, ofType: "yaml") else {
return nil
}
do {
let fileString = try String(contentsOfFile: path, encoding: .utf8)
let fileTime = date.timeIntervalSinceNow
let lines = fileString.components(separatedBy:"\n")
let end = date.timeIntervalSinceNow
@chrisbrandow
chrisbrandow / FileParse.m
Last active September 1, 2017 19:34
NSString vs. NSData Text File Parsing
- (NSArray *)stringsFromFile:(NSString *)name
{
NSDate *start = [NSDate date];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"yaml"];
NSString *fileString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSTimeInterval fileTime = [start timeIntervalSinceNow];
NSArray *lines = [fileString componentsSeparatedByString:@"\n"];
NSTimeInterval end = [start timeIntervalSinceNow];
NSLog(@"end count:%.9f %.9f, %zd", fileTime, end, offsets.count);
return lines;
@chrisbrandow
chrisbrandow / String Extensions
Created April 19, 2017 17:29
A Few Swift Extensions to make mostly indexing related tasks less painful
extension String {
var length: Int {
return self.characters.count
}
subscript (i: Int) -> String {
return self[Range(i ..< i + 1)]
}
infix operator ??? : TernaryPrecedence
infix operator ||| : AdditionPrecedence
func ???(input: String?, valuesBlock: ((String?) -> String)) -> String {
return valuesBlock(input)
}
func |||(ifNotNil: String, ifNil: String) -> ((String?) -> String) {
return { (input: String?) -> String in
if let _ = input {
@chrisbrandow
chrisbrandow / Emoji-Layout
Last active January 26, 2017 23:12
A set of operators to make Autolayout extremely readable
//: Playground - noun: a place where people can play
import UIKit
import PlaygroundSupport
///create a constraint of view to its superView
infix operator ➡️|: AdditionPrecedence
infix operator |⬅️: AdditionPrecedence
infix operator -⬆️: AdditionPrecedence
infix operator ⬇️-: AdditionPrecedence
@chrisbrandow
chrisbrandow / Big O notation
Created November 2, 2016 18:21
results testing linear search vs. binary search
import Foundation
var array = [Int]()
for i in 1 ..< 10000000 {
array.append(i)
}
//let numberList = array
extension Array where Element: Comparable {
#!/usr/bin/swift
// Swift 3.0
//
// main.swift
// CSVToTables
//
// Created by Christopher Brandow on 7/12/16.
// Copyright © 2016 flouu. All rights reserved.
//