Skip to content

Instantly share code, notes, and snippets.

@algal
algal / NonGenericWrapperCollectionViewCell.swift
Last active March 8, 2017 17:15
Non-generic WrapperCollectionViewCell
import UIKit
// known-good: Swift 3
/**
Abstract superclass for quickly defining a `UICollectionViewCell` subclass that wraps a
specific `UIView` subclass. The resulting `UICollectionViewCell` will hug the wrapped view
with layout constraints, so that both the cell and the wrapped view can influence
each other's size via the normal Auto Layout mechanism.
To use, override the class function `wrappedViewType` and the computed property
@algal
algal / VerticallyStackViewHelpers.swift
Created July 14, 2015 23:04
Vertically stacking views
/**
Adds views to containerView, along with constraints to stack them vertically
and fill horozintally.
*/
func addVerticallyStackedViews(views:[UIView], toView containerView:UIView) -> [NSLayoutConstraint]
{
for v in views {
v.setTranslatesAutoresizingMaskIntoConstraints(false)
containerView.addSubview(v)
}
@algal
algal / ImageToURL.swift
Created July 10, 2015 23:50
Save a UIImage into a URL
/// Save image in an in-memory URL
func toURL(image:UIImage) -> NSURL
{
let img = image
let imgData:NSData = UIImagePNGRepresentation(img)
let dataFormatString = "data:image/png;base64,%@"
let dataString = String(format: dataFormatString, imgData.base64EncodedStringWithOptions(.allZeros))
let dataURL = NSURL(string: dataString)!
return dataURL
}
@algal
algal / diffs.swift
Last active July 30, 2018 09:15
diff calculator
// swift 1.2
/**
:param: old array of items
:param: new array of items
:return: a tuple where with the following components:
- `newIndexesAdded` indexes in new, for elements that did not exist in old
- `oldIndexesDeleted` indexes in old, for elements not in new
@algal
algal / NSDate+RFC3339.swift
Last active January 11, 2019 21:28
NSDate+RFC3339
// known-good: Xcode 7.3 (Swift 2.2)
import Cocoa
private var rfc3339formatter:NSDateFormatter = {
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z"
formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
formatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierISO8601)!
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
@algal
algal / URLComponentsHelpers.swift
Created June 17, 2015 19:18
Fluent interface for URLComponents
private extension NSURLComponents
{
func withQueryItem(name:String,intValue:Int) -> NSURLComponents {
return self.withQueryItem(name, stringValue: NSNumber(integer: intValue).stringValue)
}
func withQueryItem(name:String,floatValue:Float) -> NSURLComponents {
return self.withQueryItem(name, stringValue: NSNumber(float: floatValue).stringValue)
}
@algal
algal / JSONService.swift
Created June 17, 2015 17:31
Helper to encapsulate loading JSON over the network.
//
// MARK: generic networking and JSON-decoding helper
//
// give it a NSURLRequest and a session, it gives you back a aresult with an AnyObject or an NSError
public class JSONService
{
/// returns JSON result on statusCode 200, and an error otherwise
public class func taskWithJSONRequest(
@algal
algal / NSString+RelativePaths.m
Created June 16, 2015 22:53
stringRelativePath
//
// NSString+RelativePaths.m
#import "NSString+RelativePaths.h"
/**
Returns a path to resourcePath, relative to anchorPath.
@algal
algal / FontUtils.swift
Last active January 12, 2018 18:43
Type-safe font initiazer
//
// MARK: Font utilities
//
enum FontFamily : String {
case HelveticaNeue = "Helvetica Neue"
case MontSerrat = "Montserrat"
}
enum FontWeight {
@algal
algal / ViewWrappingTableViewCell.swift
Last active February 2, 2016 12:37
Abstract UITableViewCell class for quickly defining UITableViewCell subclasses that merely wrap a custom UIView subclass
// ViewWrappingTableViewCell.swift
// Created by Alexis Gallagher on 6/4/15.
import UIKit
/**
Abstract superclass for quickly defining a `UITableViewCell` subclass that wraps a
specific `UIView` subclass. The resulting `UITableViewCell` will hug the wrapped view
with layout constraints, so that both the cell and the wrapped view can influence