Skip to content

Instantly share code, notes, and snippets.

@daehn
daehn / Push & Pop.swift
Last active August 29, 2015 14:16
Swift Array convenience extention
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 August 29, 2015 14:17
Debug configuration NSLog replacement
#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 August 29, 2015 14:17
Debug configuration error logging macro
#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 / vibration.m
Created April 3, 2015 11:04
Custom iOS vibration pattern with 100ms duration using private API
NSMutableDictionary *vibrationParameters = [[NSMutableDictionary alloc] init];
vibrationParameters[@"VibePattern"] = @[@YES, @100];
vibrationParameters[@"Intensity"] = @1;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wimplicit-function-declaration"
AudioServicesStopSystemSound(kSystemSoundID_Vibrate);
AudioServicesPlaySystemSoundWithVibration(kSystemSoundID_Vibrate, nil, vibrationParameters);
#pragma clang diagnostic pop
@daehn
daehn / NSString+CharacterEnumeration.m
Created May 10, 2015 15:39
Iterate over all characters of a NSString as NSStrings.
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 June 29, 2015 10:29
Levenshtein Distance calculation in a category on NSString.
@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;
public extension IteratorProtocol {
mutating public func any(_ predicate: (Element) throws -> Bool) rethrows -> Bool {
guard let current = next() else { return false }
return try predicate(current) || any(predicate)
}
mutating public func all(_ predicate: (Element) throws -> Bool) rethrows -> Bool {
guard let current = next() else { return true }
return try predicate(current) && all(predicate)
@daehn
daehn / UIFaces.swift
Last active August 29, 2015 14:25
Small UIImageView extension and download helper to quickly set a user picture from www.uifaces.com on a UIImageView
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 August 29, 2015 14:26
Struct containing the displayNames and codes for the common ISO currency codes.
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 {
@daehn
daehn / matrix.swift
Last active August 29, 2015 14:26 — forked from oisdk/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)