Skip to content

Instantly share code, notes, and snippets.

View AlanQuatermain's full-sized avatar

Jim Dovey AlanQuatermain

View GitHub Profile
@AlanQuatermain
AlanQuatermain / gist:119712
Created May 29, 2009 01:19
A lockless way of setting a lazily-initialized static global value. It works by using a CompareAndSwap atomic operation with a memory barrier to sync threads' instruction & data caches. If another thread sets the value since we looked at __staticVar, Comp
#import <libkern/OSAtomic.h>
@implementation SomeClass
+ (id) someStaticValueComputedOnFirstAccess
{
static volatile id __staticVar = nil;
if ( __staticVar == nil )
{
id var = [[Something alloc] init];
@AlanQuatermain
AlanQuatermain / gist:1120236
Created August 2, 2011 13:57
Sample code to illustrate different types of blocks— global. heap, and stack.
//
// main.m
// BlocksRuntime
//
// Created by Jim Dovey on 11-07-22.
// Copyright 2011 Jim Dovey. All rights reserved.
//
#import <Foundation/Foundation.h>
@AlanQuatermain
AlanQuatermain / insert-bsd-header-snippet
Last active September 1, 2020 21:48
A TextMate snippet to create a BSD License header for your source file.
/*
* $TM_FILENAME
* ${1:`echo "$TM_FILEPATH" | awk -F"/" '{x=NF-1}{print $x}'`}
*
* Created by `id -P | awk -F ":" '{ print $8 }'` on `date "+%d/%m/%Y"`.
*
* Copyright (c) `date +%Y` ${2:$TM_ORGANIZATION_NAME}
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@AlanQuatermain
AlanQuatermain / AQPerThreadManagedObjectContext.m
Created May 28, 2010 14:17
A set of simple wrapper functions to get per-thread NSManagedObjectContext instances. When creating main context (i.e. in app delegate) pass it to StoreManagedObjectContextForCurrentThread(). Then just call PerThreadManagedObjectContext() anywhere you nee
static NSString * const AQPerThreadManagedObjectContext = @"AQPerThreadManagedObjectContext";
void StoreManagedObjectContextForCurrentThread( NSManagedObjectContext * context )
{
[[[NSThread currentThread] threadDictionary] setObject: context forKey: AQPerThreadManagedObjectContext];
}
NSManagedObjectContext * PerThreadManagedObjectContext( void )
{
NSManagedObjectContext * result = [[[NSThread currentThread] threadDictionary] objectForKey: AQPerThreadManagedObjectContext];
@AlanQuatermain
AlanQuatermain / ChangeLicenses.rb
Created January 6, 2013 17:27
I needed to go through a large number of Xcode-generated source-code files the other day, replacing the copyright line with a license header statement. Since there were lots of files in a nested structure I decided to use a script, and I added in a glob pattern to exclude certain files/folders— because there was some public domain source in ther…
#!/usr/bin/ruby
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@AlanQuatermain
AlanQuatermain / update_package_archive.rb
Created January 6, 2013 16:52
A simple script which will change the root-paths of components in a PackageMaker document at the base of a new Xcode Archive, all without opening PackageMaker itself. Very useful for projects where you have a lot of separate components (and thus separate .xcarchive folders), which can take a while to update from the PackageMaker application afte…
#!/usr/bin/ruby
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@protocol APRemoteAddressBook <NSObject>
- (void) allPeople: (void (^)(NSArray *, NSError *)) reply;
- (void) mailingAddressesForPersonWithIdentifier: (NSString *) identifier
reply: (void (^)(NSArray *, NSError *)) reply;
- (void) emailAddressesForPersonWithIdentifier: (NSString *) identifier
reply: (void (^)(NSArray *, NSError *)) reply;
- (void) phoneNumbersForPersonWithIdentifier: (NSString *) identifier
reply: (void (^)(NSArray *, NSError *)) reply;
@end
@AlanQuatermain
AlanQuatermain / gist:2912081
Created June 11, 2012 19:15
Check whether the iOS Dev Centre is available yet, for 30 minutes
-- Thirty minute timeout
with timeout of 108000 seconds
repeat while true
delay 5
set baseurl to "https://developer.apple.com/devcenter/ios/index.action"
set curlshellcode to "curl " & baseurl
set theshellresult to do shell script curlshellcode
if theshellresult does not contain "maintenance" then
State of my main queue while running a block (at the start of main() before it all goes pear-shaped)
(lldb) expr (void) dispatch_debug((void *)dispatch_get_current_queue(), "main q")
=== log file opened for Kobo[7195] at 1337357109.056275 ===
com.apple.main-thread[0xad009600] = { xrefcnt = 0xffffffff, refcnt = 0xffffffff, suspend_cnt = 0x0, locked = 1, target = com.apple.root.default-overcommit-priority[0xad009b00], width = 0x0, running = 0x0, barrier = 1 }: main q
@AlanQuatermain
AlanQuatermain / trololol.m
Created May 18, 2012 13:39
A non-zero-based array. Guess what base it uses-- it could be anything!
@interface AQNonZeroBasedArray : NSArray
@end
@implementation AQNonZeroBasedArray
{
NSArray * _realArray;
NSUInteger _base;
}
// initialize it using AQNonZeroBasedArray * array = @[obj1, obj2, obj3];