Skip to content

Instantly share code, notes, and snippets.

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 Feb 16, 2021
FizzBuzz in Objective-C
View fizzbuzz.m
// 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;
View ContractEnforcer.h
#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 Jun 29, 2015
ld invocation that fails to link LLVM JIT
View gist:f11278d97799aebaa369
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
View mutable-objects-in-immutable-methods-on-a-mutable-runtime.m
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface Mutable : NSObject
@property (nonatomic, retain) id x;
@end
@implementation Mutable
@iamleeg
iamleeg / ObjS.swift
Created Jun 7, 2015
ObjS: now with self, _cmd arguments
View ObjS.swift
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 Jan 30, 2014
I'm not convinced I should be allowed to do this, C.
View onceperthread.m
#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 Dec 31, 2015
Does this no longer have the cycle?
View blockmaybefixed.m
#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 Dec 31, 2015
Does this actually have a retain cycle?
View blockretain.m
#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 Dec 29, 2015
UIColor literals.
View UIColor_literal.mm
#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 Dec 25, 2015
Spelunking, dispatch style
View gist:7004314
#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++)