Skip to content

Instantly share code, notes, and snippets.

//
// Set
//
struct MySet<KeyType : Hashable> : Sequence
{
var dictionaryOfItems = Dictionary<KeyType,Bool>()
init() {}
init(array:Array<KeyType>) {
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 August 29, 2015 14:10
Ye old linked list
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 August 29, 2015 14:15
Enum in a Struct vs Enum with Associated Values
/*
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 August 29, 2015 14:20
UIColor from hex, and hex is a number dammit.
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 August 29, 2015 14:22
Broken generic table view cell
/// 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)
@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 / 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 / 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 / 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
}