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 / printplist.pl
Created May 12, 2012 13:39
Using the PerlObjCBridge to display a plist file
#!/usr/bin/perl -w
use strict;
use Foundation;
my $plist = NSDictionary->dictionaryWithContentsOfFile_("/Applications/TextEdit.app/Contents/Info.plist");
print $plist->description()->UTF8String();
@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 / 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++)
@iamleeg
iamleeg / sillyloop.m
Last active December 23, 2015 23:59
You probably don't need to tell me that this is a bad idea.
#import <Foundation/Foundation.h>
typedef NS_ENUM(int, GJLRangeDirection) {
GJLRangeDirectionPositive,
GJLRangeDirectionNegative,
};
@interface GJLRange : NSEnumerator
+ (instancetype)rangeWithStart: (NSNumber *)start end: (NSNumber *)end step: (NSNumber *)step;
@iamleeg
iamleeg / gist:5644043
Last active December 17, 2015 17:09
Thoughts on how prototypical inheritance might be done in Objective-C. Notice that the Étoilé runtime (the predecessor to the GNUstep runtime) actually supports such things anyway.
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface A : NSObject
- (void)createPrivateSubclass;
@end
int main(int argc, char *argv[])
{
@autoreleasepool