Skip to content

Instantly share code, notes, and snippets.

View gist:b048f41debfa2385b23adfea5ee261dc
import Cocoa
private struct Bin<V> {
var key: Int
var value: V?
}
public class Thing<V> {
@bagelturf
bagelturf / gist:85dab3297557e1b237507460acb0ffba
Last active Apr 23, 2016
Why do I get error: cannot convert value of type 'Int' to expected argument type 'XX' on line 10?
View gist:85dab3297557e1b237507460acb0ffba
class XX {
func a(b: Int) -> Int {
return b
}
var functionA = a
func run() {
print("\(functionA(1))") // Needs (self) after functionA
View gist:7494689
non-ARC recursive block
void __block (^batchReaderRecursive)(NSUInteger position, NSUInteger count); // Used for the recursive reference
void (^batchReader)(NSUInteger position, NSUInteger count); // Used to reference the block
batchReaderRecursive = batchReader = [^(NSUInteger position, NSUInteger count) {
dispatch_async(background queue, ^{
// Do work, update position abd count
@bagelturf
bagelturf / nsdecimalnumber.m
Created Sep 17, 2013
Using NSNumberFormatter to read in decimal numbers has rounding problems. However decimalNumberWithString can't grok currency symbols, so I'm using NSNumberFormatter.
View nsdecimalnumber.m
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
NSNumberFormatter *inputFormatter = [[NSNumberFormatter alloc] init];
[inputFormatter setLocale:locale];
[inputFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[inputFormatter setLenient:YES];
[inputFormatter setGeneratesDecimalNumbers:YES];
NSDecimalNumber *a = (NSDecimalNumber *)[inputFormatter numberFromString:@"87.85"];
NSDecimalNumber *b = [NSDecimalNumber decimalNumberWithString:@"87.85"];
@bagelturf
bagelturf / gist:5543576
Created May 8, 2013
URLByAppendingPathComponent is 10x slower than URLByAppendingPathComponent:isDirectory:
View gist:5543576
Running Time Self Symbol Name
519.0ms 3.4% 1.0 -[NSURL(NSURLPathUtilities) URLByAppendingPathComponent:]
459.0ms 3.0% 0.0 -[NSURL getResourceValue:forKey:error:]
456.0ms 3.0% 0.0 CFURLCopyResourcePropertyForKey
454.0ms 2.9% 0.0 _FSURLCopyResourcePropertyForKey
2.0ms 0.0% 2.0 OSSpinLockUnlock$shim
2.0ms 0.0% 2.0 _FSURLEndResourcePropertyCacheAccess
1.0ms 0.0% 1.0 CFRunLoopGetCurrent
55.0ms 0.3% 0.0 -[NSURL(NSURLPathUtilities) URLByAppendingPathComponent:isDirectory:]
2.0ms 0.0% 1.0 -[__NSCFString hasSuffix:]
@bagelturf
bagelturf / gist:2707225
Created May 16, 2012
Crashes in CFRelease of a CALayer
View gist:2707225
Thread 1, Queue : (null)
#0 0x354b42fc in CFRelease ()
#1 0x323b090c in CA::release_objects(X::List<void const*>*) ()
#2 0x323968ec in CA::Transaction::unlock() ()
#3 0x323c8a34 in CA::Layer::~Layer() ()
#4 0x323c8928 in -[CALayer dealloc] ()
#5 0x04572d2a in -[CALayerAccessibility(SafeCategory) dealloc] ()
#6 0x323996b2 in CALayerRelease ()
#7 0x323a66c0 in CA::Layer::free_transaction(CA::Transaction*) ()