Skip to content

Instantly share code, notes, and snippets.

View bgulanowski's full-sized avatar
😱
Is the world ending? Or am I imagining it?

Brent Gulanowski bgulanowski

😱
Is the world ending? Or am I imagining it?
View GitHub Profile
//
// NSString+SnakeMaking.h
// Test
//
// Created by Brent Gulanowski on 2015-08-22.
// Copyright (c) 2015 Lichen Labs. All rights reserved.
//
#import <Foundation/Foundation.h>
@bgulanowski
bgulanowski / UIView+SubviewReplacing.m
Created December 11, 2014 13:57
Swap a subview with another view, copying the constraints from the first to the second.
@implementation UIView (SubviewReplacing)
- (void)replaceSubview:(UIView *)subview withView:(UIView *)newView
{
newView.frame = subview.frame;
[self addSubview:newView];
for (NSLayoutConstraint *constraint in [subview.constraints arrayByAddingObjectsFromArray:self.constraints]) {
id firstItem = nil;
id secondItem = nil;
if (constraint.firstItem == subview) {
@bgulanowski
bgulanowski / cocoa-collaboration.md
Last active August 29, 2015 14:02
Notes about object collaboration patterns in Cocoa

Delegate Protocols

Delegates are now the de-facto way for two objects to communicate. Delegation is good, because it ostensibly reduces coupling. An object which has a delegate can work with any object that implements the protocol. But this has led to a situation where delegate protocols are being over-relied upon. It has been used in places where coupling is unavoidable, or whether other, simpler mechanisms are available. Some protocols are also being defined before the functional requirements are even understood, leading to overly verbose protocols with methods that aren't used.

The primary case for delegation is when an object of one class needs support from an additional object to do its work successfully. The word "delegate" was originally meant specifically for this case. The first object, the delegator, uses the delegate to make decisions which the delegator can't make itself.

Notifications

If you create a delegate protocol where none of the methods actually involves delegating responsibi

@bgulanowski
bgulanowski / notification-with-block.m
Created November 21, 2013 21:42
How to use `-[NSNotificationCenter addObserverForName:object:queue:usingBlock:]`. Drop this in any Objective-C file of your choosing.
#import <libkern/OSAtomic.h>
static int32_t globalCounter;
@interface Test : NSObject
@property uint counter;
@property id observer;
@end
@bgulanowski
bgulanowski / NULevelDB_search.mm
Created November 11, 2011 16:33
NULevelDB Search
const NSUInteger MAX = 10000;
NSString *first = @"MY_KEY";
NSString *last = [NSString stringWithFormat:@"MY_KEY%c", 127]; // key beyond the last printable character '~'
__block NSUInteger biggest = 0;
__block NSString *biggestKey = nil;
[db enumerateFromKey:first toKey:last block:^(NSString *key, NSData *value) {
NSUInteger length = [value length];
@bgulanowski
bgulanowski / NULevelDB_exists.mm
Created November 11, 2011 16:32
NULevelDB Exists
BOOL exists = [myDB storedValueExistsForKey:@"country"];
@bgulanowski
bgulanowski / NULevelDB_delete.mm
Created November 11, 2011 16:31
NULevelDB Delete
BOOL success = [myDB deleteStoredValueForKey:@"age"];
@bgulanowski
bgulanowski / NULevelDB_load.mm
Created November 11, 2011 16:30
NULevelDB load
id address = [myDB storedValueForKey:@"address"];
@bgulanowski
bgulanowski / NULevelDB_insert.mm
Created November 11, 2011 16:28
NULevelDB insert
BOOL success = [myDB storeValue:@"Brent Gulanowski" forKey:@"name"];
@bgulanowski
bgulanowski / NULDBDB_init.mm
Created November 11, 2011 16:27
create a new NULevelDB database
NULDBDB *myDB = [[[NULDBDB alloc] initWithLocation:@"/tmp" bufferSize:(1<<22)] autorelease];