Skip to content

Instantly share code, notes, and snippets.

View ddeville's full-sized avatar
🐈

Damien Deville ddeville

🐈
View GitHub Profile
@ddeville
ddeville / gist:1186439
Created September 1, 2011 15:38
Recursively print UIView hierarchy for a particular view
- (void)printViewHierarchy:(UIView *)view
{
if (view == nil)
return;
NSLog(@"%@", view);
for (UIView *subview in [view subviews])
{
[self printViewHierarchy: subview];
@ddeville
ddeville / gist:1527517
Created December 28, 2011 10:40
MIME type to UTI and back again in Cocoa
#if TARGET_OS_IPHONE
#import <MobileCoreServices/MobileCoreServices.h>
#else
#import <CoreServices/CoreServices.h>
#endif
/*
MIME type to UTI
*/
NSURLResponse *response = ... // assume a URL response from somewhere else.
@ddeville
ddeville / gist:5228224
Last active December 15, 2015 08:08 — forked from joshaber/gist:5228077
@interface RetrievalOperation : NSOperation
+ (id)retrievalOperationWithBlock:(id (^)(void))block;
@property (readonly, strong, nonatomic) id result;
@end
@interface RetrievalOperation (/* Private */)
@property (readwrite, strong, nonatomic) id result;
@property (copy, nonatomic) id (^block)(void);
@end
@ddeville
ddeville / gist:5670475
Last active December 17, 2015 20:49
Caution: this is very very nasty...
@interface SomeViewController : UIViewController
@end
@implementation SomeViewController
- (IBAction)show:(id)sender
{
UIImagePickerController *pickerController = [UIImagePickerController new];
[pickerController setDelegate:(id)self];
@ddeville
ddeville / ruby-nil-messaging-objc.m
Created June 28, 2013 14:00
Ruby-like nil messaging in Objective-C
//
// Created by Damien DeVille on 6/23/13.
// Copyright (c) 2013 Damien DeVille. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
extern id _objc_setNilReceiver(id newNilReceiver);
@ddeville
ddeville / x86_64-registers
Created June 29, 2013 12:21
x86_64 registers
rax - accumulator
rbx - base
rcx - count
rdx - data
r8-15
rsi - source index
rdi - destination index
rbp - base pointer
@ddeville
ddeville / gist:6078492
Last active December 20, 2015 05:29
objc_msgSend cast
So:
id boxedValue = objc_msgSend(self, mappingSelector, instance, value, boxingParameters);
would become:
id boxedValue = ((id (*)(id, SEL, id, id, id))objc_msgSend)(self, mappingSelector, instance, value, boxingParameters);
The first `id` and `SEL` are basically `self` and `_cmd`, the first 2 arguments in any objc_msgSend.
@ddeville
ddeville / gist:7097600
Created October 22, 2013 09:21
The correct way to react to changes on observed objects.
//
// main.m
// KVO
//
// Created by Damien DeVille on 10/22/13.
// Copyright (c) 2013 Damien DeVille. All rights reserved.
//
#import <Foundation/Foundation.h>
@ddeville
ddeville / gist:7103822
Created October 22, 2013 16:32
Think of a parent view controller that contains two children view controllers. The title of the parent view controller is a mixture of the titles of its children. The title of the second view controller depends on the name of its represented object too. Now, one would only need to bind the title of the window (for example) to the parent view con…
@interface ParentViewController : NSViewController
@property (strong, nonatomic) FirstChildViewController *firstViewController;
@property (strong, nonatomic) SecondChildViewController *secondViewController;
@end
@implementation ParentViewController
@ddeville
ddeville / gist:7169443
Last active December 26, 2015 14:59
+keyPathsForValuesAffectingValueForKey: vs +keyPathsForValuesAffecting<Key>.
//
// main.m
// KVO
//
// Created by Damien DeVille on 10/26/13.
// Copyright (c) 2013 Damien DeVille. All rights reserved.
//
#import <Foundation/Foundation.h>