Skip to content

Instantly share code, notes, and snippets.

@bjhomer
bjhomer / sendbinary.html
Created April 27, 2012 04:20 — forked from ericktai/sendbinary.html
Send Binary File with StackMob
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript" src="http://static.stackmob.com/js/json2-min.js"></script>
<script type="text/javascript" src="http://static.stackmob.com/js/underscore-1.3.0-min.js"></script>
<script type="text/javascript" src="http://static.stackmob.com/js/backbone-0.5.3-min.js"></script>
<script type="text/javascript" src="http://static.stackmob.com/js/stackmob-js-0.1.1-min.js"></script>
<script type="text/javascript">

Javascript closure capture for Objective-C developers

In Javascript, you'll often see things like this:

for (var i=0; i<5; ++i) {
  var someString = strings[i];

  button.onclick = (function (str) {
    return function (event) {

alert(str);

-(UIImage *)sidebar:(HSImageSidebarView *)sidebar imageForIndex:(NSUInteger)anIndex {
return [myArrayOfImages objectAtIndex:anIndex];
}
- (NSUInteger)countOfImagesInSidebar:(HSImageSidebarView *)sidebar {
return [myArrayOfImages count];
}
@bjhomer
bjhomer / block_return.m
Created June 4, 2012 16:39
'return' meaning different things semantically
// This is just a toy example, but it demonstrates the semantic
// overloading of the 'return' keyword when blocks are in play.
// Note that I'm not saying this is confusing, per se - I'm just
// saying that it adds cognitive load as I look at the method and
// try to figure out which 'return' statements actually represent
// an early return.
- (Person *)findPersonWithName:(NSString *)name {
NSArray *families;
if (name.length == 0) {
NSView *view;
// view.hidden == YES right now
// view.alpha = 0.0 right now;
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
context.duration = 0;
view.hidden = NO;
}];
@bjhomer
bjhomer / cards.py
Created October 2, 2012 20:20
drawing cards
import sys
import random
if __name__ == "__main__":
cardcount = int(sys.argv[1])
drawcount = 8
cards_not_seen = set(range(cardcount))
@bjhomer
bjhomer / CoreDataAsynchrononus.m
Created October 5, 2012 20:25
Core data asynchronous fetching API proposal
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface CDAsynchronousArray : NSArray
// Provides access to the full count of objects. May be slow, e.g. if the underlying data store
// is only providing paginated access to objects. May not even be available, as in a search
// engine result set.
- (void)accessCountWithHandler:(void (^)(NSUInteger count, NSError *))countHandler;
@bjhomer
bjhomer / warnings.m
Created November 1, 2012 23:06
Capturing 'self' leads to a retain cycle inside methods starting with -add
- (void)testAddRemove
{
if (arc4random() % 2) {
NSLog(@"A");
[_other addWithDispatchBlock: ^{
NSLog(@"self: %@", self); // <-- warning about capturing 'self'
}];
}
else {
NSLog(@"B");
@bjhomer
bjhomer / PresentModallyInsideNavigation.m
Created November 2, 2012 22:49
Convenience method for presenting a modal view controller inside a navigation controller
@implementation UIViewController (NavigationPresentation_additions)
- (void)presentViewController:(UIViewController *)viewControllerToPresent inNavigationControllerAnimated:(BOOL)flag completion:(void (^)(void))completion {
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewControllerToPresent];
[self presentViewController:navController animated:flag completion:completion];
}
@end
@bjhomer
bjhomer / pov results.txt
Last active December 10, 2015 19:28
Wheel of Time POV scenes by character. (Compiled by @tommetge and @bjhomer. Data from http://www.encyclopaedia-wot.org. Fetched using https://gist.github.com/bjhomer/4489273.)
Rand => 237
Perrin => 153
Egwene => 130
Mat => 115
Elayne => 83
Nynaeve => 61
Min => 35
Faile => 28
Aviendha => 23
Galad => 22