Skip to content

Instantly share code, notes, and snippets.

@nicklockwood
nicklockwood / ARCHelper.h
Last active November 20, 2018 10:02
ARC Helper
//
// ARC Helper
//
// Version 2.2
//
// Created by Nick Lockwood on 05/01/2012.
// Copyright 2012 Charcoal Design
//
// Distributed under the permissive zlib license
// Get the latest version from here:
@nicklockwood
nicklockwood / UINavigationBar+CustomBackground.m
Created January 23, 2012 10:45
Customise UINavigationBar background on iOS 4 & 5
@implementation UINavigationBar (CustomBackground)
- (UIImage *)barBackground
{
return [UIImage imageNamed:@"top-navigation-bar.png"];
}
- (void)didMoveToSuperview
{
//iOS5 only
@nicklockwood
nicklockwood / Evil.h
Last active December 10, 2015 11:58
An evil idea for writing a metalanguage on top of Objective C where the syntax is actually pleasant. Nobody should ever, ever do this.
//Interface
@interface NSObject (Evil)
@property (readonly) BOOL (^equals)(id object);
@end
@interface NSArray (Evil)
@nicklockwood
nicklockwood / Async JSON request
Created February 9, 2013 08:58
Async networking on iOS
NSURLrequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/file.json"]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
}];
@nicklockwood
nicklockwood / gist:7446228
Last active December 28, 2015 04:59
Why having a single success/error callback block is better than having separate ones

Suppose you have a network method that only uses a single block callback

typedef void (^Handler)(BOOL success, id response, NSError *error);

- (void)makeRequestWithHandler:(Handler)handler;

But you have to make this request dozens of times, and you want to reuse the same failure handler in most cases? Easy, just have a factory function that returns a block:

typedef void (^SuccessHandler)(id response);

typedef void (^ErrorHandler)(NSError *error);

@nicklockwood
nicklockwood / gist:7447381
Last active February 14, 2017 09:31
Why I still prefer nibs to storyboards.

Storyboard Segues initially seem like a pretty cool way to construct interfaces using minimal glue code. But actually, ordinary nibs already support this, and in a much more flexible way.

Certainly, a Storyboard lets you bind a button action up to display a view controller with no code, but in practice you will usually want to pass some data to the new controller, depending on which button you used to get there, and this means implementing the -prepareForSegue:sender: method, which rapidly becomes a giant if/elseif statement of doom, negating most of the benefit of the codeless segue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"modalSegue"])
    {
        ModalViewController *controller = (ModalViewController *)segue.destination;

controller.someProperty = someValue;

@nicklockwood
nicklockwood / gist:7559729
Last active April 27, 2016 16:18
A proposal for an alternative NSNotificationCenter API that doesn't suck

Method

- (void)addObserver:(id)observer
            forName:(NSString *)name
             object:(id)object
              queue:(NSOperationQueue *)queue
         usingBlock:(void (^)(NSNotification *note, __weak id observer))block;

Usage

@nicklockwood
nicklockwood / NSURL+FastCoding.m
Created December 12, 2013 14:24
How to add NSURL support for FastCoding v 2.0 (this is a giant hack and will probably be unnecessary in a future release)
@interface FCURL : NSObject
@property (nonatomic, copy) NSString *absoluteString;
@end
@implementation NSURL (FastCoding)
- (Class)classForCoder
{
@nicklockwood
nicklockwood / NSCacheFix.m
Last active December 31, 2015 18:18
NSCache seems to be broken in iOS 7 - it doesn't empty itself, even if your app runs out of memory. This is a simple category to fix the problem.
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
@implementation NSCache (Fix)
+ (void)load
{
//swizzle init
Method a = class_getInstanceMethod(self, @selector(init));
@nicklockwood
nicklockwood / gist:8537947
Last active January 3, 2016 23:49
The Perfect Language

It's Impossible

I have reluctantly come to accept that it is impossible to create a "perfect" programming language. Every language is domain-specific in some sense, and many of the criteria that make a language good for one purpose are fundamentally in opposition to qualities that are good for another.

A classic example would be "scripting" languages versus "embedded" languages.

Good qualities in a scripting language are:

  • Dynamic typing (no need to specify types, or cast between them)