Skip to content

Instantly share code, notes, and snippets.

@landonf
landonf / BlockMock.h
Created February 7, 2010 07:00
Using blocks to quickly transpose method implementations
#import <Foundation/Foundation.h>
typedef void (^BlockMockMethod)(NSInvocation *);
@interface BlockMock : NSProxy {
/* Instance to use as the mock's default target */
id _target;
/* Map of NSString selectors to BlockMockMethod blocks */
NSMutableDictionary *_mocks;
#import <Foundation/Foundation.h>
@protocol PropertyProtocol
@property(readonly) NSString *example;
@end
int main (int argc, char *argv[]) {
id<PropertyProtocol> prop = nil;
/* Property access works fine here. */
#import <Foundation/Foundation.h>
/*
* File fails to compile with an warning in the ExtendedPropertyClass:
* test.m:46: warning: property ‘property’ requires method '-property' to be
* defined - use @synthesize, @dynamic or provide a method implementation
*
* If ExtendedPropertyClass inherits the -method implementation from
* PropertyClass, shouldn't it also inherit the -property implementation?
*/
@landonf
landonf / block-copy.m
Created May 22, 2010 17:38
Demonstrates how -copy and -retain are handled for different block types (stack, heap, global).
/*
* Demonstrates how -copy and -retain are handled for different block types (stack, heap, global).
*/
#import <Foundation/Foundation.h>
/* print a block -description to stdout */
void PrintBlock (NSString *name, id b) {
NSString *str;
str = [NSString stringWithFormat: @"%@ (%@):\n\t-retain\t%@\n\t-copy\t%@\n\n", b, name, [[b retain] autorelease], [[b copy] autorelease]];
/**
* An abstract interface that provides purely asynchronous, read-only access to stream-based data.
*
* TODO: Define end-of-stream handling
*
* @par Thread Safety
* Not Thread-safe. May not be shared across threads.
*/
@protocol PLInputStream
/*
* If you #import this in every file that uses blocks -- or place
* it in a prefix header -- the block runtime symbol references will
* be weak linked. Attempting to copy a block will cause a crash
* if the block runtime isn't available.
*
* Use #ifdef __BLOCKS__ to guard the block APIs in your headers so that
* they don't cause a parse error with incompatible compilers
*/
@landonf
landonf / bw-test.sh
Created July 10, 2010 17:53
Simple example of using ipfw to simulate poor network conditions. Handy for mobile performance testing.
# Packet loss rate (percentage, 0.0-1.0)
PLR=0.25
# Delay in ms
DELAY=300
# Host to filter
HOST=example.com
ipfw pipe 1 config bw 800Kbit plr "$PLR" delay "$DELAY"
@landonf
landonf / type-infer.m
Created October 27, 2010 17:53
An Objective-Not-C with type inference might look like ...
@implementation Logger
- (void) writeToLog: (NSString) message {
NSLog("%@", message);
}
@end
/* infer the types */
val printer = ^(str) {
[self writeToLog: str];
}
@landonf
landonf / apple-actorkit-methods
Created January 5, 2011 21:55
Symbol dump of classes/methods from Apple's ActorKit
landonf@onefish:~> nm /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.2.sdk/System/Library/PrivateFrameworks/ActorKit.framework/ActorKit| grep '\[' | cut -c 12-
+[AKDeferredReply currentReply]
+[AKDeferredReply initialize]
+[AKDispatchQueue currentRunLoopDispatchQueue]
+[AKDispatchQueue migrantDispatchQueue]
+[AKDispatchQueue newThreadRunLoopDispatchQueue]
+[AKMailbox mailboxWithTarget:protocol:]
-[AKActor dealloc]
-[AKActor deferredReply]
-[AKActor init]
Plausible CrashReporter now provides support for executing your own code in the context of the
crash reporter's signal handler, after the crash report has been written to disk. This was a
regularly requested feature, and provides a convenient point for performing application finalization.
However, there is an important caveat to be aware to when writing code intended for execution inside
of a signal handler -- specifically, your code must be async-safe.
Program Flow and Signal Handlers
When the signal handler is called, the normal flow of the program is interrupted, and your program is
an entirely unknown state. Locks may be held, the heap may be corrupt (or in the process of being updated),