Skip to content

Instantly share code, notes, and snippets.

View iamleeg's full-sized avatar
📬
New mailing list for software engineering discussion: https://sicpers.curated.co

Graham Lee iamleeg

📬
New mailing list for software engineering discussion: https://sicpers.curated.co
View GitHub Profile
@iamleeg
iamleeg / onceperthread.m
Created January 30, 2014 13:07
I'm not convinced I should be allowed to do this, C.
#import <Foundation/Foundation.h>
#include <pthread.h>
void logExactlyOnce(void)
{
static __thread dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"Only done once?");
});
}
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface Mutable : NSObject
@property (nonatomic, retain) id x;
@end
@implementation Mutable
@iamleeg
iamleeg / gist:f11278d97799aebaa369
Created June 29, 2015 20:36
ld invocation that fails to link LLVM JIT
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.3.0
Thread model: posix
"/Applications/Xcode_6_3_2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.9.0 -syslibroot /Applications/Xcode_6_3_2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -o /Users/leeg/Library/Developer/Xcode/DerivedData/ClassBrowser-gmbtduezhzgclfddsisfkakriqlj/Build/Products/Debug/ClassBrowser.app/Contents/MacOS/ClassBrowser -L/Users/leeg/Library/Developer/Xcode/DerivedData/ClassBrowser-gmbtduezhzgclfddsisfkakriqlj/Build/Products/Debug -L/usr/local/lib -filelist /Users/leeg/Library/Developer/Xcode/DerivedData/ClassBrowser-gmbtduezhzgclfddsisfkakriqlj/Build/Intermediates/ClassBrowser.build/Debug/ClassBrowser.build/Objects-normal/x86_64/ClassBrowser.LinkFileList -lLLVMLTO -lLLVMObjCARCOpts -lLLVMLinker -lLLVMBitWriter -lLLVMIRReader -lLLVMBPFCodeGen -lLLVMBPFDesc -lLLVMBPFInfo -lLLVMBP
@iamleeg
iamleeg / gist:1466859
Created December 12, 2011 12:09
Using Eero from Xcode 4

Use this as the "custom script" for a build rule for files matching *.ero:

/path/to/clang-eero-prebeta/bin/clang -S -o ${DERIVED_FILES_DIR}/${INPUT_FILE_BASE}.s ${INPUT_FILE_PATH}

The output file should be $(DERIVED_FILES_DIR)/$(INPUT_FILE_BASE).s. Xcode automatically assembles and links the output into the binary.

@iamleeg
iamleeg / FZAUIElement.m
Created December 20, 2011 21:04
Use one selector to provide many methods with similar implementations.
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface FZAUIElement : NSObject
- (id)initWithAXUIElement: (AXUIElementRef)anElement;
- (BOOL)press: (NSError *__autoreleasing*)error;
- (BOOL)cancel: (NSError *__autoreleasing*)error;
@end
@iamleeg
iamleeg / README.md
Created February 3, 2012 18:16
Causing methods generated by AppCode to throw exceptions

Go to "File Templates" in the Preferences window, then under the 'code' tab choose "OC implemented method body" and paste the template method in there. This then causes AppCode to generate methods like this:

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
    [[NSException exceptionWithName:NSInternalInconsistencyException reason:@"Unimplemented method" userInfo:nil] raise];
    return 0;
    //To change the template use AppCode | Preferences | File Templates.
    
}

Another approach would be to use the usual forwarding approach by putting this as the first line:

@iamleeg
iamleeg / pileOfPoo.m
Created March 22, 2012 01:51
The pile of poo Objective-C method
#import <Foundation/Foundation.h>
@interface A: NSObject
@end
@implementation A
void pileOfPoo(id self, SEL _cmd) {
NSLog(@"💩");
}
@iamleeg
iamleeg / gist:2214389
Created March 27, 2012 09:34
Nested C array
int main(int argc, char *argv[]) {
int x[5][5][5];
int i, j, k;
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
for (k = 0; k < 5; k++) {
x[i][j][k] = k + 5 * j + (5*5*i);
}
}
}
Undefined symbols for architecture x86_64:
"_Printf", referenced from:
_main in ccFkWgAM.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
$ vulcan build -c "PREFIX=/opt/libobjc2; mkdir $PREFIX; make" -p /opt/libobjc2 -v -o "$TMPDIR/libobjc2.tgz"
Packaging local directory... done
Uploading source package... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<style type="text/css">
html, body, iframe { margin: 0; padding: 0; height: 100%; }
iframe { display: block; width: 100%; border: none; }
</style>