Skip to content

Instantly share code, notes, and snippets.

View beccadax's full-sized avatar

Becca Royal-Gordon beccadax

View GitHub Profile
@beccadax
beccadax / NSArray+Conjunction.h
Created August 9, 2013 01:04
Category to turn @[ @"a", @"b", @"c" ] into @"a, b and c". So useful I must have it in three different apps.
//
// NSArray+Conjunction.h
// Typesetter
//
// Created by Brent Royal-Gordon on 2/11/12.
// Copyright (c) 2012 Architechies. All rights reserved.
//
@beccadax
beccadax / NPKPropertyListRepresentation.h
Created September 13, 2013 06:15
Update of my previous TsPropertyListRepresentation. Any object which conforms to the NPKPropertyListRepresentation protocol automatically gains a number of methods for converting it to and from a plist. You can control what's serialized by overriding +keysForPropertyListRepresentation, and how it's done by overriding various other class methods.…
//
// NSObject+TsPropertyListRepresentation.h
// PressKit
//
// Created by Brent Royal-Gordon on 3/6/13.
// Copyright (c) 2013 Novelty Publishers. All rights reserved.
//
#import <Foundation/Foundation.h>
@beccadax
beccadax / GBSFileWrapper.h
Created September 26, 2013 11:29
NSFileWrapper is really convenient, but has a lot of pain points. Here's my design for a replacement.
// There's a lot that bothers me about NSFileWrappers:
// * Directories are mutable, but regular files and symlinks aren't.
// * You can't change from one type to another.
// * ...but the -readFromURL:... method can violate both of these rules. Probably.
// * Wrappers include a .filename, but this is only relevant to their containing folders.
// * Wrappers include a .preferredFilename, but this is only relevant to the folder they're being added to, and doesn't matter after being added.
// * There's no way to replace a file wrapper in a directory, except by removing the old one and then adding the new one.
// Here's how I would design a file wrapper replacement.
typedef NS_ENUM(NSUInteger, GBSFileWrapperType) {
#import <Foundation/Foundation.h>
@interface Foo : NSObject
@property NSUInteger bar;
- (void)doWithBar;
@end
@beccadax
beccadax / async-iteration.m
Created April 4, 2014 01:15
This code sample performs an async operation (+postsForBook:completion:) on an array of UHUBook objects. It seems like you should be able to extract this iteration pattern somehow.
+ (void)postsForBooks:(NSArray*)books completion:(void (^)(NSArray *, NSError *))completion {
[self postsForBook:books.firstObject otherBooks:[books subarrayWithRange:NSMakeRange(1, books.count - 1)] completion:completion];
}
+ (void)postsForBook:(UHUBook *)book otherBooks:(NSArray*)others completion:(void (^)(NSArray *, NSError *))completion {
if(book == nil) {
completion(@[], nil);
}
@beccadax
beccadax / NSProgress+Ignored.h
Created April 5, 2014 01:51
Sometimes NSProgress captures the progress of an operation you couldn't care less about. Here's a way to handle that.
//
// NSProgress+Ignored.h
// Uhura
//
// Created by Brent Royal-Gordon on 4/4/14.
// Copyright (c) 2014 Architechies. All rights reserved.
//
#import <Foundation/Foundation.h>
@beccadax
beccadax / NSAttributedString+format.m
Created May 5, 2014 20:28
Do simple +stringWithFormat:-type operations with NSAttributedString.
//
// NSAttributedString+format.m
// Chatterbox
//
// Created by Brent Royal-Gordon on 2/7/14.
// Copyright (c) 2014 Architechies. All rights reserved.
//
#import "NSAttributedString+format.h"
@beccadax
beccadax / stored-class-vars.swift
Created June 8, 2014 06:49
Workaround for current lack of stored class variables in Swift.
struct Statics {
static var foo: Int = 0
}
class var foo: Int {
get { return Statics.foo }
set { Statics.foo = newValue }
}
@beccadax
beccadax / untitled.swift
Created June 12, 2014 05:06
Implementations of first() and last() functions in Swift.
func first<S: Sequence> (seq: S) -> S.GeneratorType.Element? {
var gen = seq.generate()
return gen.next()
}
func first<S: Sequence> (seq: S, #count: Int) -> Slice<S.GeneratorType.Element> {
var gen = seq.generate()
var list = Slice<S.GeneratorType.Element>()
while list.count < count {
@beccadax
beccadax / dispatch-sync-safe.swift
Created June 13, 2014 19:50
In reply to @rnapier, here's how to reduce the implicit optionals in your code: factor them out.
func result() -> A {
var result : A = dispatch_sync(resultQueue, {
return self.result
})
return result
}
func dispatch_sync<R> (queue: dispatch_queue_t, block: Void -> R ) -> R {
var result: R!
dispatch_sync(queue) {