Skip to content

Instantly share code, notes, and snippets.

View JensAyton's full-sized avatar

Jens Ayton JensAyton

View GitHub Profile
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface Test: NSObject
@end
@interface Test (Dynamic)
- (void)foo:(float)x :(float)y;
@JensAyton
JensAyton / oops.c
Last active August 29, 2015 13:56 — forked from davepeck/oops.c
One example of cleanup without using goto. (This isn't an opinion on what Apple “should have” done, it’s just an answer to a specific question on Twitter. It would have done nothing to avoid the actual bug.) I also removed the unused variable “signedHashes”.
static OSStatus
SSLVerifySignedServerKeyExchange(SSLContext *ctx, bool isRsa, SSLBuffer signedParams,
uint8_t *signature, UInt16 signatureLen)
{
SSLBuffer hashCtx;
hashCtx.date = 0;
OSStatus err = SSLVerifySignedServerKeyExchangeInner(ctx, isRsa, signedParams, signature, signatureLen, &hashCtx);
SSLFreeBuffer(hashCtx);
@JensAyton
JensAyton / JADictionaryOfVariableBindings.h
Created April 28, 2014 22:28
NSDictionaryOfVariableBindings at compile time, just because.
#import <Foundation/Foundation.h>
#define JADictionaryOfVariableBindings(...) \
[NSDictionary dictionaryWithObjects:(const id []){ __VA_ARGS__ } \
forKeys:(const NSString *[]){ JATEMPLATE_MAP(JATEMPLATE_NAME_FROM_ARG, __VA_ARGS__) } \
count:JATEMPLATE_ARGUMENT_COUNT(__VA_ARGS__)]
#define JATEMPLATE_NAME_FROM_ARG(ITEM) @#ITEM
@JensAyton
JensAyton / gist:4c6df3f1ca7d32c15fef
Created June 9, 2014 07:13
try() for discriminated-union error handling in Swift
import Foundation
// Because the real thing crashes
enum Result {
case Value(Any)
case Error(NSError?)
}
enum
{
kVKC_Shift = 56,
kVKC_Option = 58,
kVKC_Control = 59,
kVKC_rShift = 60, /* Right-hand modifiers; not implemented */
kVKC_rOption = 61,
kVKC_rControl = 62,
kVKC_Command = 55,
}
// Struct handler from an @encode() string parser for a debug printer.
#import <objc/runtime.h> // For @encode syntax constants.
static void DecodeStruct(const char * const encoding, size_t * const cursor, const void ** const buffer, NSMutableString *string)
{
NSCParameterAssert(encoding != NULL && encoding[*cursor] == _C_STRUCT_B && cursor != NULL && buffer != NULL);
#if INCLUDE_STRUCT_NAMES
@JensAyton
JensAyton / BlockActionHack.m
Created September 21, 2010 14:17
Hack to enable the use of block ivars as IBAction targets. Stuffs a bunch of nastiness in NSObject, but transparenty works for classes implemeting actions: create a property of type BlockAction, assign a block to it, and use the property name (with a trai
/*
Hack to enable the use of block ivars as IBAction targets. Stuffs a bunch
of nastiness in NSObject, but transparenty works for classes implemeting
actions: create a property of type BlockAction, assign a block to it,
and use the property name (with a trailing colon) as the target selector.
Did I mention it's a hack? There are two major problems:
* Type safety. It assumes any block property will have the correct
signature. Can be partially worked around when compiling with Clang,
which embeds @encode() strings into blocks, but this is undocumented.
@JensAyton
JensAyton / objc_dep.py
Created January 6, 2011 22:03
Header dependency mapper for Objective-C. This fork randomly colours arcs, for better tracing of spaghetti.
#!/usr/bin/python
# Nicolas Seriot
# 2011-01-06
# http://github.com/nst/objc_dep
"""
Input: path of an Objective-C project
Output: import dependancies Graphviz format
// There are exact equivalents for CoreFoundation, if you like typing CFRelease a lot.
NSString *path = [[NSBundle mainBundle] builtInPlugInsPath]; // Or privateFrameworksPath, or some other thing.
path = [path stringByAppendingPathComponent:@"foo.dylib"];
void *handle = dlopen([path fileSystemRepresentation], flags);
@JensAyton
JensAyton / arc-exception-funhouse.m
Created October 22, 2011 17:08
Fun with ARC and exceptions
/*
The desired output of each test case is:
<MyException 0xPTR>{ TestName, retain count 1 } created.
<MyException 0xPTR>{ TestName, retain count 1 } caught.
<MyException 0xPTR>{ TestName, retain count 1 } dealloced.
For a fun time, try to predict the actual output for all six test cases!