Skip to content

Instantly share code, notes, and snippets.

@davbeck
davbeck / NSImage+TMStretchable.h
Created January 12, 2012 02:14
A category to draw an NSImage using 9 slice stretching
//
// NSImage+TMStretchable.h
// ThinkMessenger
//
// Created by David Beck on 1/11/12.
// Copyright (c) 2012 ThinkUltimate. All rights reserved.
//
#import <AppKit/AppKit.h>
@davbeck
davbeck / Example
Created May 14, 2012 21:49
CGContextClipToDrawing is a CGContext function for clipping with drawing in a block.
- (void)drawRect:(CGRect)drawRect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextClipToDrawing(context, self.bounds, ^(CGContextRef maskContext, CGRect rect){
UIGraphicsPushContext(maskContext);
[[UIColor whiteColor] setFill];
CGContextFillRect(maskContext, rect);
@davbeck
davbeck / gist:3661211
Created September 6, 2012 23:19
NSTimer with block
_actionDelayTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:[NSBlockOperation blockOperationWithBlock:^{
NSLog(@"Well this is useless.");
}] selector:@selector(start) userInfo:nil repeats:YES];
@davbeck
davbeck / NSObject+TUBlockDealloc.h
Created November 29, 2012 01:23
Execute block on dealloc
//
// NSObject+TUBlockDealloc.h
// ThinkGV
//
// Created by David Beck on 4/20/12.
// Copyright (c) 2012 ThinkUltimate LLC. All rights reserved.
//
#import <Foundation/Foundation.h>
@davbeck
davbeck / README.md
Last active August 29, 2015 13:57
TNKPropertyKey

It's become common practice to use an @selector for associated objects. This is useful because not only are SELs guaranteed to have a unique address, but because as of Xcode 5, Xcode will warn you when you use a selector that it does not know about. Meaning that it is somewhat protected against spelling mistakes.

This can also be useful when using string keys. For things like state restoration, where you want a key for a given property, you can use the selector for the getter, and get a string from that. Xcode will autocomplete the name for you, and will warn you if you misspell it.

- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
    [super encodeRestorableStateWithCoder:coder];
    
 [coder encodeObject:self.feedID forKey:TNKPropertyKey(feedID)];
@davbeck
davbeck / formatted_mapping.json
Created October 2, 2014 17:07
Android/Rails Time Zone Mapping
{
"International Date Line West":{
"offset":-39600,
"abbreviation":"SST",
"name":"Pacific/Midway"
},
"Midway Island":{
"offset":-39600,
"abbreviation":"SST",
"name":"Pacific/Midway"
@davbeck
davbeck / time_zone_map.rb
Created October 2, 2014 17:09
Generate Time Zone Mapping
#!/usr/bin/env ruby
#encoding: utf-8
require 'active_support/all'
timezones = ActiveSupport::TimeZone::MAPPING.map { |rails_name, name|
zone = ActiveSupport::TimeZone.new(name)
{
rails_name => {
offset: zone.utc_offset,
@davbeck
davbeck / WKUIDelegate.m
Last active September 28, 2023 06:59
Boilerplate implementation of WKUIDelegate to support Javascript alerts.
#pragma mark - WKUIDelegate
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)())completionHandler
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
message:message
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
completionHandler();
@davbeck
davbeck / TNKURLFormatter.swift
Created February 6, 2015 19:43
TNKURLFormatter
//
// TNKURLFormatter.swift
//
// Created by David Beck on 2/6/15.
// Copyright (c) 2015 ThinkUltimate. All rights reserved.
//
import Foundation
@davbeck
davbeck / GQL.swift
Created May 2, 2015 15:52
GraphQL data structure implemented in Swift
import Foundation
protocol GQLNodeArgument {}
extension String: GQLNodeArgument {}
extension NSNumber: GQLNodeArgument {}
class GQLNode: StringLiteralConvertible, ArrayLiteralConvertible, Printable, DebugPrintable {
let name: String?