Skip to content

Instantly share code, notes, and snippets.

Avatar

Stuart Carnie stuartcarnie

View GitHub Profile
@stuartcarnie
stuartcarnie / PerfTester.h
Created Jan 25, 2010
Micro-benchmarks for iPhone and OS X
View PerfTester.h
//
// PerfTester.h
// TollFreeBridingTests
//
// Created by Stuart Carnie on 1/24/10.
// Copyright 2010 Manomio. All rights reserved.
//
#import <Foundation/Foundation.h>
View link-plblocks.sh
@stuartcarnie
stuartcarnie / BlocksAdditions.h
Created Sep 8, 2010
A few Objective-C blocks additions for NSArray
View BlocksAdditions.h
@interface NSArray(BlocksAdditions)
- (NSArray*)mapUsingBlock:(id (^)(id obj))block;
- (NSSet*)mapToSetUsingBlock:(id (^)(id obj))block;
- (id)firstUsingBlock:(BOOL(^)(id obj))block;
@end
@stuartcarnie
stuartcarnie / NSObject+Blocks.h
Created Apr 24, 2011
Blocks category for NSObject
View NSObject+Blocks.h
// credits to Matthias Plappert
#import <Foundation/Foundation.h>
@interface NSObject (PWBlocks)
- (void)performBlock:(void (^)(void))block afterDelay:(NSTimeInterval)delay;
@end
@stuartcarnie
stuartcarnie / gist:945862
Created Apr 28, 2011
Preload a Core Text font descriptor if you are having performance issues in iOS 4.3
View gist:945862
#import <CoreText/CoreText.h>
...
// preload
dispatch_queue_t queue = dispatch_queue_create("com.company.worker", NULL);
dispatch_async(queue, ^(void) {
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:@"Helvetica" forKey:(id)kCTFontFamilyNameAttribute];
[attributes setObject:[NSNumber numberWithFloat:36.0f] forKey:(id)kCTFontSizeAttribute];
@stuartcarnie
stuartcarnie / checkdev.sh
Created Jun 6, 2011
Ping developer.apple.com and growl when it's up
View checkdev.sh
#!/bin/sh
read -p "Apple Id: " appleid; echo
stty -echo
read -p "Password: " passw; echo
stty echo
while [ /bin/true ]; do
curl -u$APPLEID:$PASSW developer.apple.com/devcenter/ios/index.action | grep "soon"
if [ $? -eq 1 ]; then
growlnotify -m "Done"
@stuartcarnie
stuartcarnie / nestedBlocks.m
Created Jul 6, 2011
Nested blocks discussion
View nestedBlocks.m
void RetrieveFriends(GameCenter& gameCenter, GKLocalPlayer* localPlayer)
{
[localPlayer loadFriendsWithCompletionHandler:^(NSArray* friends, NSError* error)
{
[GKPlayer loadPlayersForIdentifiers:friends withCompletionHandler:^(NSArray* players, NSError* error2)
{
dispatch_async(dispatch_get_main_queue(), ^(void)
{
TestLala(gameCenter); //, friends, players, error2);
});
@stuartcarnie
stuartcarnie / NestedBlocks.m
Created Jul 6, 2011
Nested blocks discussion
View NestedBlocks.m
void RetrieveFriends(GameCenter& gameCenter, GKLocalPlayer* localPlayer)
{
[localPlayer loadFriendsWithCompletionHandler:^(NSArray* friends, NSError* error)
{
// 1. this block is copied and retained by loadFriendsWithCompletionHandler
// 2. When called, loadPlayersForIdentifiers:withCompletionHandler will copy and retain the block below,
// which doesn't exist until the outer block is called
[GKPlayer loadPlayersForIdentifiers:friends withCompletionHandler:^(NSArray* players, NSError* error2)
{
// Finally, dispatch_async will copy and retain the block which is passed to it below
@stuartcarnie
stuartcarnie / SCKeyboardHardwareChanged.c
Created Aug 2, 2011
Code to detect when hardware keyboard is attached / detached
View SCKeyboardHardwareChanged.c
#include <objc/runtime.h>
// this is called when a keyboard is attached / detached, and
// isHardwareAttached will return YES / NO appropriately
void hardwareChangeIMP(id aSelf, SEL sel, BOOL isHardwareAttached) {
SEL newSel = NSSelectorFromString(@"_modeChange:");
[aSelf performSelector:newSel withObject:[NSNumber numberWithBool:isHardwareAttached]];
}
// call this to start capturing hardware changes
@stuartcarnie
stuartcarnie / gist:1314463
Created Oct 25, 2011
Example of non-determinism of finalizers. This will crash with ObjectDisposedException on v4.0 x64 and x86 runtimes.
View gist:1314463
using System;
namespace FinalizerTest
{
class Program
{
static void Main(string[] args)
{
for (var i = 0; i < 10000; i++)
{
You can’t perform that action at this time.