Skip to content

Instantly share code, notes, and snippets.

View stuartcarnie's full-sized avatar

Stuart Carnie stuartcarnie

View GitHub Profile
@stuartcarnie
stuartcarnie / main.m
Created September 16, 2012 04:30
tail call with fake msgSend in Objective C
#import <Foundation/Foundation.h>
#import <objc/message.h>
extern id fake_msgSend(id, SEL, int) __attribute__((noinline));
@interface Foo : NSObject
- (id)foo:(int)n;
@end
@stuartcarnie
stuartcarnie / increment_version.py
Created August 2, 2012 22:48
Increment CFBundleVersion of the specified plist file
#!/bin/env python
# Copyright (c) 2012 Stuart Carnie
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
@stuartcarnie
stuartcarnie / grabmsg.py
Created June 6, 2012 00:30
grab raw messages from gmail / imap
#!/usr/bin/env python
import imaplib
import getpass
import os
import argparse
import sys
__productname__ = 'grabmsg'
__version__ = "0.1"
@stuartcarnie
stuartcarnie / UIAlertView_BlocksExtension.h
Created February 20, 2012 05:49
UIAlertView block extension
#import <UIKit/UIKit.h>
@interface UIAlertView (BlocksExtension)<UIAlertViewDelegate>
+ (id)alertViewWithTitle:(NSString *)title message:(NSString *)message completionBlock:(void (^)(NSUInteger buttonIndex))block cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;
@end
@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++)
{
@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 / 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 / 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 / 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 / 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];