Skip to content

Instantly share code, notes, and snippets.

View johnclayton's full-sized avatar

John Clayton johnclayton

View GitHub Profile
@johnclayton
johnclayton / ImageLoader.swift
Created April 1, 2022 17:01
Closure based image loader
import UIKit
import Combine
final class ImageLoader {
typealias LoadPublisher = AnyPublisher<UIImage, Error>
typealias Cache = NSCache<CacheKey, UIImage>
private let loadImage: (URL, Cache) -> LoadPublisher
private let cachedImage: (URL, Cache) -> UIImage?
import Foundation
extension UserDefaults {
struct Key {
typealias RawValue = String
let rawValue: RawValue
init(_ string: String) {
self.rawValue = string
}
@johnclayton
johnclayton / QSandbox
Last active December 19, 2015 09:58
A convenient wrapper around the sandbox for OSX and iOS apps
@implementation QSandbox
+ (NSString *) documentsDirectory {
return [self directoryInUserSearchPath:NSDocumentDirectory];
}
+ (NSString *) applicationSupportDirectory {
return [self directoryInUserSearchPath:NSApplicationSupportDirectory];
}
- (NSString *) MD5Hash {
const char *cStr = [self UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5( cStr, (CC_LONG)strlen(cStr), result );
NSMutableString *hash = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for (int i = 0; i < 16; i++) {
[hash appendFormat: @"%02x", result[i]];
}
+ (NSString *)nibName {
return [self className];
}
+ (id) loadFromNib {
return [self loadFromNibWithOwner:self];
}
+ (id) loadFromNibWithOwner:(id)owner {
NSArray *theObjects = [[NSBundle mainBundle] loadNibNamed:[self nibName] owner:owner options:nil];
@johnclayton
johnclayton / gist:4468810
Created January 6, 2013 17:28
NSArray first element convenience
@interface NSArray (FSQFoundation)
- (id) firstObject; ///< @returns the first object if there is one, or nil.
@end
@implementation NSArray (FSQFoundation)
- (id) firstObject {
if (self.count < 1) {
@johnclayton
johnclayton / gist:4468789
Created January 6, 2013 17:23
NSString empty checks
@interface NSString (FSQFoundation)
+ (BOOL) isEmpty:(NSString *)string;
+ (BOOL) isNotEmpty:(NSString *)string;
@end
@implementation NSString (FSQFoundation)
+ (BOOL) isEmpty:(NSString *)string {
- (UITextField *) textField {
UITextField *textField = nil;
for (id subview in [self subviews]) {
if ([subview isKindOfClass:[UITextField class]]) {
textField = (UITextField *)subview;
break;
}
}
return textField;
}
@johnclayton
johnclayton / FSQOpenStruct.h
Created February 22, 2012 16:04
An implementation of Ruby's OStruct in Objective C
//
// FSQOpenStruct.h
// FivesquareKit
//
// Created by John Clayton on 2/5/12.
// Copyright (c) 2012 Fivesquare Software, LLC. All rights reserved.
//
#import <Foundation/Foundation.h>

Welcome to Drift!

Drift is an always-already versioned, cloud-backed text editor. You can use it to take notes, and save them in the GitHub cloud.

Your gists are always saved locally, and any changes you make will get pushed to GitHub's servers.

To name a gist, touch its name in the toolbar.

You can use the share button at the top-right to copy a link to one of your gists, or view it on the web in Safari.