Skip to content

Instantly share code, notes, and snippets.

Avatar

Chris Brandow chrisbrandow

View GitHub Profile
@chrisbrandow
chrisbrandow / centeredCollectionViewCells
Last active May 17, 2019
CollectionView with centered cells
View centeredCollectionViewCells
/// 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 Mar 26, 2018
Eidhof's Autolayout DSL with a couple additions
View autolayoutDSL
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 Dec 1, 2017
Objective-C dispatch Queue Speed Tests
View lockSpeedTests.m
- (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 Sep 1, 2017
benchmarking String vs. Data reading and processing.
View FileParse.swift
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 Sep 1, 2017
NSString vs. NSData Text File Parsing
View FileParse.m
- (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 Apr 19, 2017
A Few Swift Extensions to make mostly indexing related tasks less painful
View String Extensions
extension String {
var length: Int {
return self.characters.count
}
subscript (i: Int) -> String {
return self[Range(i ..< i + 1)]
}
View if-let Ternary operator
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 Jan 26, 2017
A set of operators to make Autolayout extremely readable
View Emoji-Layout
//: 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 Nov 2, 2016
results testing linear search vs. binary search
View Big O notation
import Foundation
var array = [Int]()
for i in 1 ..< 10000000 {
array.append(i)
}
//let numberList = array
extension Array where Element: Comparable {
View CSV to Markdown
#!/usr/bin/swift
// Swift 3.0
//
// main.swift
// CSVToTables
//
// Created by Christopher Brandow on 7/12/16.
// Copyright © 2016 flouu. All rights reserved.
//