Skip to content

Instantly share code, notes, and snippets.

Avatar

Silvan Dähn daehn

View GitHub Profile
@daehn
daehn / FibonacciGenerator.swift
Last active Aug 25, 2015
Swift Generator that produces Fibonacci numbers
View FibonacciGenerator.swift
import UIKit
struct FibonacciGenerator: GeneratorType, SequenceType {
var (f1, f2) = (1, 1)
mutating func next() -> Int? {
let result = f1
(f1, f2) = (f2, f1 + f2)
return result
@daehn
daehn / Push & Pop.swift
Last active Aug 29, 2015
Swift Array convenience extention
View Push & Pop.swift
extension Array {
mutating func pop() -> T? {
return self.removeAtIndex(0)
}
mutating func push(element: T) {
self.append(element)
}
}
@daehn
daehn / DEBUG_LOG.h
Last active Aug 29, 2015
Debug configuration NSLog replacement
View DEBUG_LOG.h
#ifndef DEBUG_LOG
#ifdef DEBUG
#define DEBUG_LOG(fmt, ...) NSLog(fmt, ##__VA_ARGS__)
#else
#define DEBUG_LOG(fmt, ...)
#endif
#endif
@daehn
daehn / ERROR_LOG.h
Last active Aug 29, 2015
Debug configuration error logging macro
View ERROR_LOG.h
#ifndef ERROR_LOG
#ifdef DEBUG
#define ERROR_LOG(error) if(error){NSLog(@"%s Error: %@", __PRETTY_FUNCTION__, error);}
#else
#define ERROR_LOG(error)
#endif
#endif
@daehn
daehn / NSString+CharacterEnumeration.m
Created May 10, 2015
Iterate over all characters of a NSString as NSStrings.
View NSString+CharacterEnumeration.m
typedef void(^StringEnumerationBlock)(NSString *characterString, NSUInteger index, BOOL *stop);
- (void)enumerateCharactersUsingBlock:(StringEnumerationBlock)block {
NSParameterAssert(block);
BOOL stop = NO;
for (NSUInteger i = 0; i < self.length; i++) {
const unichar character = [self characterAtIndex:i];
NSString *characterString = [NSString stringWithCharacters:&character length:1];
block(characterString, i, &stop);
if (stop) {
@daehn
daehn / NSString+Levenshtein.m
Created Jun 29, 2015
Levenshtein Distance calculation in a category on NSString.
View NSString+Levenshtein.m
@implementation NSString (Levenshtein)
- (NSUInteger)lev_distanceToString:(NSString *)comparisonString {
NSString *referenceString = [self copy];
NSUInteger referenceLength = referenceString.length;
NSUInteger comparisonLength = comparisonString.length;
unsigned long m, n;
unsigned int distanceMatrix[comparisonLength + 1][referenceLength + 1];
distanceMatrix[0][0] = 0;
@daehn
daehn / UIFaces.swift
Last active Aug 29, 2015
Small UIImageView extension and download helper to quickly set a user picture from www.uifaces.com on a UIImageView
View UIFaces.swift
import UIKit
typealias JSON = [String: AnyObject]
public typealias ImageRequestCompletion = (UIImage?, NSError?) -> Void
public enum UserImageSize: String {
case Epic = "epic"
case Big = "bigger"
case Normal = "normal"
case Tiny = "mini"
@daehn
daehn / CurrencyDataSource.swift
Last active Aug 29, 2015
Struct containing the displayNames and codes for the common ISO currency codes.
View CurrencyDataSource.swift
struct Currency {
let code, displayName: String
init?(code: String?) {
if let code = code,
displayName = NSLocale.systemLocale().displayNameForKey(NSLocaleCurrencyCode, value:code) {
self.code = code
self.displayName = displayName
} else {
View matrix.swift
public struct MatrixIndex: BidirectionalIndexType {
public let x, y : Int
private let columns: Int
public func successor() -> MatrixIndex {
return (x + 1 == columns) ?
MatrixIndex(x: 0, y: y + 1, columns: columns) :
MatrixIndex(x: x + 1, y: y, columns: columns)
@daehn
daehn / optional-assignment.swift
Last active Aug 29, 2015 — forked from radex/optional-assignment.swift
Optional assignment operator implementation. `foo ?= bar` will evaluate and assign bar to foo if and only if foo is nil. Equivalent to `||=` in Ruby and other languages.
View optional-assignment.swift
infix operator ?= {
associativity right
precedence 90
assignment
}
func ?=<T>(inout variable: T?, expr: @autoclosure () -> T) {
if variable == nil {
variable = expr()
}