Skip to content

Instantly share code, notes, and snippets.

@cdzombak
cdzombak / gist:16351244498cdb2f383c
Created February 1, 2015 21:33
Effective Organization for Developers, talk abstract

You have a lot of responsibilities and plenty of to-dos to keep track of. Add in your email and Instapaper and it can feel impossible to make any progress. But it's not! In this talk we’ll discuss basic principles you can use to organize yourself, stay atop your responsibilities, and destress at the same time. We’ll also cover some common organizational pitfalls that we as programmers can fall into, and we’ll briefly discuss tooling, including the pros and cons of using a tool like OmniFocus.

@cdzombak
cdzombak / as.m
Created February 26, 2015 18:55
swift-style as in ObjC
#import <Foundation/Foundation.h>
#define as_checked(EXPR, KLASS) ({ id _obj = EXPR; NSCAssert([_obj isKindOfClass:[KLASS class]], @"Cannot cast %@ to %@", NSStringFromClass([_obj class]), NSStringFromClass([KLASS class])); _obj; })
#define as_option(EXPR, KLASS) ({ id _obj = EXPR; if (![_obj isKindOfClass:[KLASS class]]) _obj = nil; _obj; })
@interface NSObject(As)
- (instancetype)asChecked:(Class)klass;
@cdzombak
cdzombak / lazy_get.m
Created February 27, 2015 04:44
simple ObjC lazy getter macro
#import <Foundation/Foundation.h>
#define lazy_get(TYPE, NAME, VALUE) \
@synthesize NAME = _##NAME; \
- (TYPE)NAME { if (!_##NAME) _##NAME = (VALUE); return _##NAME; }
// example usage follows:
@interface LazyThing : NSObject
//
// CDZWeak.h
// https://gist.github.com/cdzombak/844e887ed4bdb933a905
//
// Created by Chris Dzombak on 3/22/15.
// Copyright (c) 2015 Chris Dzombak. All rights reserved.
//
#ifndef CDZWeak_h
#define CDZWeak_h
NYTCompletionBlock completion = ^(id result) {};
id completionMock = [OCMockObject partialMockForObject:completion];
[[[completionMock expect] copy] andReturn:completion];
id sut = [[NYTXXX alloc] initWithCompletion:completionMock];
#pragma unused (sut)
[completionMock verify];
@cdzombak
cdzombak / NSArray.h
Last active August 29, 2015 14:21
NSArray thought experiment. This code may not compile and there may be subtle bugs/inconsistencies. But this serves to illustrate the possible solution outlined in https://www.dzombak.com/blog/2015/05/Cocoa-s-mutable-subclass-pattern-is-an-antipattern.html .
@protocol NSArray
+ (instancetype)array;
+ (instancetype)arrayWithObject:(id)anObject;
+ (instancetype)arrayWithObjects:(const id [])objects count:(NSUInteger)cnt;
+ (instancetype)arrayWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
+ (instancetype)arrayWithArray:(NSArray *)array;
- (instancetype)initWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
- (instancetype)initWithArray:(NSArray *)array;
@cdzombak
cdzombak / swift2errors.md
Last active August 29, 2015 14:23
an executive summary of my ever-forthcoming "error handling in swift 2" blog post. https://twitter.com/cdzombak/status/611704414810832896
  • throws in Swift 2 is just another way to return from a function/method. Because of its propagation semantics, it's not really "exceptions", which is probably a good change.
  • But unlike an Either or Result return type, throws in Swift 2 cannot communicate type information.
  • This sucks; and we will realize it sucks when we keep having to refer to documentation every time we call a function that throws to see what sort of errors we may have to handle. This is part of the problem that types solve; they are documentation, with compiler-enforced guarantees.
  • Arguments based on the clunkiness of pattern-matching on a Result type vs. the new syntax are not valid. Had the Swift team chosen to introduce a blessed stdlib Result type, they could have introduced elegant syntax to handle it without requiring boilerplate; remember, everything related to throws is new syntax.
  • Yes, this would make versioning harder. Changing the types of errors you return changes the contract of an API, if you care abou
@cdzombak
cdzombak / keyboard-autolayout.m
Last active August 29, 2015 14:25
iOS Keyboard Avoiding with Auto Layout
// in -loadView or similar…
self.completeButtonBottomConstraint = [NSLayoutConstraint constraintWithItem:self.completeButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:v attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-1.0*IDPCompleteButtonBottomSpacing];
// in -viewWillAppear:…
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
- (void)keyboardWillShow:(NSNotification *)note {
[self updateBottomConstraintFromNotification:note];
}
@cdzombak
cdzombak / CDZObjectWatcher.h
Created July 25, 2015 15:44
Watching Core Data objects for updates or deletions
//
// CDZObjectWatcher.h
//
// Created by Chris Dzombak.
// Copyright (c) 2015 Chris Dzombak. All rights reserved.
//
@import Foundation;
@import CoreData;