Skip to content

Instantly share code, notes, and snippets.

View beccadax's full-sized avatar

Becca Royal-Gordon beccadax

View GitHub Profile
#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 / 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) {
@beccadax
beccadax / untitled.swift
Created June 24, 2014 01:44
Swift Core Data stack built with lazy properties. Warning: untested.
// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
@lazy var managedObjectContext: NSManagedObjectContext = {
let context = (NSManagedObjectContext() as NSManagedObjectContext?)!
context.persistentStoreCoordinator = self.persistentStoreCoordinator
return context
}()
// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
@beccadax
beccadax / findLast.swift
Created July 11, 2014 00:56
Broken findLast() function
func findLast<C : Swift.Collection where C.GeneratorType.Element : Equatable>(domain: C, value: C.GeneratorType.Element) -> C.IndexType? {
return reduce(enumerate(domain), nil) { (prevIndex: C.IndexType?, pair: (index: C.IndexType, elem: C.GeneratorType.Element)) -> C.IndexType? in
if pair.elem == value {
return pair.index
}
else {
return prevIndex
}
}
// It doesn't like the combine (closure) parameter:
@beccadax
beccadax / realEnumerate.swift
Last active August 29, 2015 14:03
This function creates an index/element tuple generator that actually uses the right indices, but it fails with a *really* weird error.
func realEnumerate<C: Swift.Collection>(collection: C) -> GeneratorOf<(C.IndexType, C.GeneratorType.Element)> {
var i: C.IndexType = collection.startIndex
return GeneratorOf() {
if i < collection.endIndex {
// 'i' is flagged in comparison with "'C.IndexType' is not convertible to 'CString'". Whuh?
let value = (i, collection[i])
i = i.successor()
return value
}
// DO NOT EDIT. This file is machine-generated and constantly overwritten.
// Make changes to <$managedObjectClassName$>.swift instead.
import CoreData
<$if noninheritedAttributes.@count > 0$>
enum <$managedObjectClassName$>Attributes: String {<$foreach Attribute noninheritedAttributes do$>
case <$Attribute.name$> = "<$Attribute.name$>"<$endforeach do$>
}
<$endif$>