This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (id)sharedWhatever | |
{ | |
static Whatever *whatever = nil; | |
if( !whatever ) | |
{ | |
Whatever *newWhatever = [[self alloc] init]; | |
if( !OSAtomicCompareAndSwapPtrBarrier( nil, newWhatever, (void *)&whatever ) ) | |
{ | |
[newWhatever release]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (id)sharedInstance { | |
id singleton = objc_getAssociatedObject(...); | |
if(!singleton) { | |
[lock lock]; | |
singleton = objc_getAssociatedObject(...); | |
if(!singleton) | |
objc_setAssociatedObject([[self alloc] init], ...); | |
[lock unlock]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define BIT_SET(array, bit) (array[bit / 8] |= (1 << (bit % 8))) | |
#define BIT_CLEAR(array, bit) (array[bit / 8] &= ~(1 << (bit % 8))) | |
#define BIT_GET(array, bit) ((array[bit / 8] | (1 << (bit % 8))) != 0) | |
uint8_t array[5]; | |
bzero(array, sizeof(array); | |
BIT_SET(array, 18); | |
BIT_GET(array, 18); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import <pthread.h> | |
typedef void (^MyBlock)(void); | |
typedef void (*MyFunc)(void *); | |
static void Block(MyBlock block) | |
{ | |
NSLog(@"Block called with %p", block); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (id)fooWithError: (NSError **)outError | |
{ | |
id obj = [self barWithThing: @"thing" error: outError]; | |
if(!obj) | |
obj = [self quuxWithThing: @"thing" error: outError]; | |
return obj; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NSURLConnection wrapper | |
// like NSURLConnection, requires a runloop, callbacks happen in runloop that set up load | |
@interface LDURLLoader : NSObject | |
{ | |
NSURLConnection *_connection; | |
NSTimeInterval _timeout; | |
NSTimer *_timeoutTimer; | |
NSURLResponse *_response; | |
long long _responseLengthEstimate; | |
NSMutableData *_accumulatedData; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
// .h file | |
struct MyConstantsStruct | |
{ | |
NSString *foo; | |
NSString *bar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface NSFileReference : NSObject { | |
// internal storage would probably be NSURL, but could be | |
// NSString holding a path, FSRef, alias data, whatever | |
} | |
- (id)initWithPath: (NSString *)path; | |
- (id)initWithFileSystemRepresentation: (const char *)path; | |
- (id)initWithFSRef: (FSRef *)ref; | |
- (id)initWithAliasData: (NSData *)data; | |
- (id)initWithURL: (NSURL *)url; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dispatch_block_t RecursiveBlock(void (^block)(dispatch_block_t recurse)) | |
{ | |
// assuming ARC, so no explicit copy | |
return ^{ block(RecursiveBlock(block)); }; | |
} | |
typedef void (^OneParameterBlock)(id parameter); | |
OneParameterBlock RecursiveBlock1(void (^block)(OneParameterBlock recurse, id parameter)) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// function | |
void with(id obj, void (^block)(id obj)) | |
{ | |
block(obj); | |
} | |
// example use | |
with(blah, ^(Blah *blah) { | |
// use blah here | |
}); |
OlderNewer