Skip to content

Instantly share code, notes, and snippets.

View stuartcarnie's full-sized avatar

Stuart Carnie stuartcarnie

View GitHub Profile
@stuartcarnie
stuartcarnie / PerfTester.h
Created January 25, 2010 06:01
Micro-benchmarks for iPhone and OS X
//
// PerfTester.h
// TollFreeBridingTests
//
// Created by Stuart Carnie on 1/24/10.
// Copyright 2010 Manomio. All rights reserved.
//
#import <Foundation/Foundation.h>
@stuartcarnie
stuartcarnie / BlocksAdditions.h
Created September 8, 2010 10:01
A few Objective-C blocks additions for NSArray
@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 April 24, 2011 16:57
Blocks category for NSObject
// 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 April 28, 2011 05:38
Preload a Core Text font descriptor if you are having performance issues in iOS 4.3
#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 June 6, 2011 19:30
Ping developer.apple.com and growl when it's up
#!/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 July 6, 2011 06:16
Nested blocks discussion
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 July 6, 2011 06:17
Nested blocks discussion
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 August 2, 2011 08:33
Code to detect when hardware keyboard is attached / detached
#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 October 25, 2011 21:54
Example of non-determinism of finalizers. This will crash with ObjectDisposedException on v4.0 x64 and x86 runtimes.
using System;
namespace FinalizerTest
{
class Program
{
static void Main(string[] args)
{
for (var i = 0; i < 10000; i++)
{