Skip to content

Instantly share code, notes, and snippets.

Avatar

Alexis Gallagher algal

  • San Francisco
View GitHub Profile
View SwiftSet.swift
//
// Set
//
struct MySet<KeyType : Hashable> : Sequence
{
var dictionaryOfItems = Dictionary<KeyType,Bool>()
init() {}
init(array:Array<KeyType>) {
View gist:f64814fc5b307124e165
class Bag<T: Hashable>: Sequence, Printable {
var _storage = Dictionary<T, Int>()
typealias GeneratorType = Dictionary<T,Int>.GeneratorType
func addItem(item: T) {
if let count = _storage[item] {
_storage[item] = count + 1
} else {
_storage[item] = 1
}
@algal
algal / List.swift
Last active Aug 29, 2015
Ye old linked list
View List.swift
class Box<T> {
let unbox:T
init(unbox:T) { self.unbox = unbox }
}
/*
Boxing is necessary because Swift does not allow recursive enum types.
However, AFAICT, is it also desirable here because it enables structural
@algal
algal / StructEnumVsEnumAssocValues.swift
Last active Aug 29, 2015
Enum in a Struct vs Enum with Associated Values
View StructEnumVsEnumAssocValues.swift
/*
The examples below show two ways to model a state machine with two states,
On and Off, with an additional constant Int property.
On -> Off and Off -> On transitions must be inititated by calling flip()
*/
@algal
algal / UIColor+Hex.swift
Last active Aug 29, 2015
UIColor from hex, and hex is a number dammit.
View UIColor+Hex.swift
extension UIColor
{
convenience init(RGBValue:UInt32)
{
let red = (RGBValue & 0xFF0000) >> 16
let green = (RGBValue & 0x00FF00) >> 8
let blue = (RGBValue & 0x0000FF) >> 0
self.init(red:CGFloat(red)/255.0,green:CGFloat(green)/255.0,blue:CGFloat(blue)/255.0,alpha:CGFloat(1.0))
}
}
@algal
algal / GenericTableViewCell.swift
Last active Aug 29, 2015
Broken generic table view cell
View GenericTableViewCell.swift
/// Not sure why this generic version does not work.
private class WrapperTableViewCell<T:UIView> : UITableViewCell
{
class var classReuseIdentifier:String { return T.self.description() + "CellIdentifier" }
override class func requiresConstraintBasedLayout() -> Bool { return true }
override init(style: UITableViewCellStyle, reuseIdentifier: String?)
{
super.init(style: UITableViewCellStyle.Default, reuseIdentifier: reuseIdentifier)
View NSString+RelativePaths.m
//
// NSString+RelativePaths.m
#import "NSString+RelativePaths.h"
/**
Returns a path to resourcePath, relative to anchorPath.
@algal
algal / JSONService.swift
Created Jun 17, 2015
Helper to encapsulate loading JSON over the network.
View JSONService.swift
//
// 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 / URLComponentsHelpers.swift
Created Jun 17, 2015
Fluent interface for URLComponents
View URLComponentsHelpers.swift
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 / ImageToURL.swift
Created Jul 10, 2015
Save a UIImage into a URL
View ImageToURL.swift
/// 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
}