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 / fizzbuzz.m
Created February 16, 2021 21:31
FizzBuzz in Objective-C
// hey, we've all got a coding interview to crack, right?
#import <Foundation/Foundation.h>
@interface FZBZSparseArray : NSArray
- initWithCount:(NSUInteger)count placeholder:placeholder overrides:overrides;
@end
@interface FZBZWrappedArray : NSArray
- initWrappingArray:underlyingArray;
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface ContractEnforcer<T> : NSProxy
- (T)initWithTarget:(T)target;
+ (T)enforcerWithTarget:(T)target;
@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
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface Mutable : NSObject
@property (nonatomic, retain) id x;
@end
@implementation Mutable
@iamleeg
iamleeg / ObjS.swift
Created June 7, 2015 01:03
ObjS: now with self, _cmd arguments
import Cocoa
typealias Selector = String
enum IMP {
case accessor((Selector->IMP, Selector)->((Selector)->IMP)?)
case asInteger((Selector->IMP, Selector)->Int?)
case methodMissing((Selector->IMP, Selector)->((Selector)->IMP)?)
case mutator((Selector->IMP, Selector, Selector->IMP)->Void)
case description((Selector->IMP, Selector)->String?)
@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?");
});
}
@iamleeg
iamleeg / blockmaybefixed.m
Last active December 31, 2015 00:59
Does this no longer have the cycle?
#import <Foundation/Foundation.h>
@interface A : NSObject
- (void)doThingWithObject:(id)o completion:(void(^)(void))completionHandler;
@end
@implementation A
- (void)doThingWithObject:(id)o completion:(void(^)(void))completionHandler
{
NSLog(@"object: %@", o);
@iamleeg
iamleeg / blockretain.m
Last active December 31, 2015 00:59
Does this actually have a retain cycle?
#import <Foundation/Foundation.h>
@interface A : NSObject
- (void)doThingWithObject:(id)o completion:(void(^)(void))completionHandler;
@end
@implementation A
- (void)doThingWithObject:(id)o completion:(void(^)(void))completionHandler
{
NSLog(@"object: %@", o);
@iamleeg
iamleeg / UIColor_literal.mm
Last active December 29, 2015 03:09
UIColor literals.
#import <UIKit/UIKit.h>
UIColor * operator"" _c(unsigned long long color)
{
unsigned long long redComponent = (color & 0xff0000 >> 16);
unsigned long long greenComponent = (color & 0x00ff00) >> 8;
unsigned long long blueComponent = color & 0xff;
float red = redComponent / 255.0;
float green = greenComponent / 255.0;
float blue = blueComponent / 255.0;
@iamleeg
iamleeg / gist:7004314
Last active December 25, 2015 16:09
Spelunking, dispatch style
#import <Foundation/Foundation.h>
#import <dispatch/dispatch.h>
#import <objc/runtime.h>
void printMethodList(Class cls, const char *prefix)
{
const char *className = class_getName(cls);
unsigned int countOfMethods;
Method *methodList = class_copyMethodList(cls, &countOfMethods);
for(unsigned int i = 0; i < countOfMethods; i++)