Skip to content

Instantly share code, notes, and snippets.

@rustle
rustle / gist:4678755
Created January 31, 2013 00:28
Couple of typedefs that I think make the NSFilePresenter block APIs a little easier to read
typedef void (^FilePresenterReacquirerBlock)(void);
typedef void (^FilePresenterReaderBlock)(FilePresenterReacquirerBlock reacquirer);
typedef void (^FilePresenterWriterBlock)(FilePresenterReacquirerBlock reacquirer);
typedef void (^FilePresenterCompletionHandler)(NSError *errorOrNil);
- (void)relinquishPresentedItemToReader:(FilePresenterReaderBlock)reader
{
// Do stuff here ahead of reading
reader(^{
// Do stuff here after reading is done
@rustle
rustle / gist:5017569
Last active December 14, 2015 02:59
Example of an NSValue subclass with a return value on getValue:
#import <Foundation/Foundation.h>
@interface RSTLValue : NSValue
- (BOOL)getValue:(void *)value expectedSize:(size_t)expectedSize;
@end
struct Foo {
NSUInteger i;
char * bar;
};
@rustle
rustle / gist:5027734
Created February 25, 2013 04:23
An accessible UISlider subclass that closely emulates the time scrubber in Music.app
//
// KATGShowControlsScrubber.m
// KATG
//
// Created by Doug Russell on 2/24/13.
// Copyright (c) 2013 Doug Russell. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// ARCLogic.h
//
#ifndef ARCLOGIC
#define ARCLOGIC
#ifdef HASARC
#undef HASARC
#endif
@rustle
rustle / gist:5079872
Created March 4, 2013 04:14
Example of prefixing a list with a leading character. In this case, adding a • to the front of each line.
NSString *someText = ...;
NSMutableString *someLines = [NSMutableString new];
[someText enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
// Make sure the line isn't just whitespace
NSMutableString *mutableLine = [line mutableCopy];
CFStringTrimWhitespace((__bridge CFMutableStringRef)mutableLine);
if ([mutableLine length])
{
[someLines appendFormat:@" • %@\n", line];
}
@rustle
rustle / gist:5112576
Created March 7, 2013 22:49
Example of an ESHTTPOperation subclass for downloading a file directly to disk using NSOutputStream including resuming
//
// KATGDownloadOperation.h
// KATG
//
// Created by Doug Russell on 3/7/13.
// Copyright (c) 2013 Doug Russell. All rights reserved.
//
#import "ESHTTPOperation.h"
@rustle
rustle / gist:5556512
Created May 10, 2013 18:46
Seems like ACAccountType needs a better isEqual: implementation
// Works fine
accounts = [accounts filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(ACAccount *evaluatedObject, NSDictionary *bindings) {
return [[[evaluatedObject accountType] identifier] isEqualToString:[self.accountType identifier]];
}]];
// No dice
accounts = [accounts filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(ACAccount *evaluatedObject, NSDictionary *bindings) {
return [[evaluatedObject accountType] isEqual:self.accountType];
#pragma mark - Memory Cache Subscripting
// Allow self[key] for looking up and writing to memory cache
// This is mostly a novelty
- (id)objectForKeyedSubscript:(id)key
{
return [self.memoryCache objectForKey:key];
}
@rustle
rustle / gist:6406932
Created September 1, 2013 19:58
Example of the kind of thorough checks you can do in asserts that would be too expensive to leave in production code
#if !defined(NS_BLOCK_ASSERTIONS)
#define RSTLSetOfStringsAssert(set) [set enumerateObjectsUsingBlock:^(id obj, BOOL *stop) { NSParameterAssert([obj isKindOfClass:[NSString class]]); }];
#else
#define RSTLSetOfStringsAssert(set)
#endif
@rustle
rustle / gist:6618451
Created September 19, 2013 02:32
A __attribute__ decorator that causes method calls to become implicit dispatches could be interesting: __attribute__((dispatch, async, priority))
#import <Foundation/Foundation.h>
#include <objc/message.h>
typedef void (*DISPATCH_IMP_NOARGS_NORETURN)(id, SEL);
typedef id (*DISPATCH_IMP_NOARGS)(id, SEL);
@interface Foo : NSObject
@end