Skip to content

Instantly share code, notes, and snippets.

# Mac OS X
*.DS_Store
# Xcode
*.pbxuser
*.mode1v3
*.mode2v3
*.perspectivev3
*.xcuserstate
project.xcworkspace/
@nicklockwood
nicklockwood / gist:e940105c6c0a62b6a0b7
Last active August 29, 2015 14:06
Mutable collection worst case
//here is an array factory method
@interface Foo: NSObject
@end
@implementation Foo
+ (NSArray *)newArray
{
@nicklockwood
nicklockwood / gist:54c87830ed80de6eae27
Created October 25, 2014 19:35
Combining CryptoCoding and FastCoding
- (void)writeObject:(id)object toFile:(NSString *)file withPassword:(NSString *)password
{
NSData *data = [FastCoder dataWithRootObject:object];
CryptoArchive *archive = [[CryptoArchive alloc] initWithRootObject:data password:password];
data = [FastCoder dataWithRootObject:archive];
[data writeToFile:file atomically:YES];
}
- (id)readObjectFromFile:(NSString *)file withPassword:(NSString *)password
{
@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 / 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)