Skip to content

Instantly share code, notes, and snippets.

View zachwaugh's full-sized avatar

Zach Waugh zachwaugh

View GitHub Profile
@zachwaugh
zachwaugh / switch-shortcut.swift
Last active February 6, 2023 16:15
Swift shortcut for returning and/or assigning the result of switch statement?
// Can currently do this
func titleForSection1(section: Int) -> String? {
switch section {
case 0: return "Foo"
case 1: return "Bar"
default: return nil
}
}
// But I want to do this to remove the redundant returns
@zachwaugh
zachwaugh / gist:521133
Created August 12, 2010 15:19
Loading rails environment in a script
#!/usr/bin/env ruby
# Load Rails
ENV['RAILS_ENV'] = ARGV[0] || 'production'
DIR = File.dirname(__FILE__)
require DIR + '/../config/environment'
# ... do stuff that requires using your rails models
@zachwaugh
zachwaugh / Makefile
Last active October 6, 2020 03:36
Swift + Sublime Text 3
# I rarely use make, probably a better way to do some/all of this?
SDK=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk
SOURCES=main.swift
EXECUTABLE=main
all:
swiftc -sdk $(SDK) $(SOURCES) -o $(EXECUTABLE)
clean:
@zachwaugh
zachwaugh / iOS-devices-graphic.md
Last active August 19, 2019 15:16
iOS device compatibility

A hopefully accurate list of iOS compatibility per device

iOS 13

ios13

iOS 12

ios12

iOS 11

ios11

@zachwaugh
zachwaugh / ZWDictionaryOfVariableBindings.m
Last active July 29, 2016 12:39
Experiment to figure out how NSDictionaryOfVariableBindings works, and try to make a more flexible version. This version strips underscores and supports self.* variables, but I'm sure there is a better way to do it.
#import <Foundation/Foundation.h>
// Experimental improvement to NSDictionaryOfVariableBindings where keys are simplified to remove underscores and "self." prefixes
// so you can use the simple version within the VFL string
//
// Example:
//
// [NSLayoutConstraint constraintsWithVisualFormat:@"|-[_foo]-[self.bar]-[baz]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_foo, self.bar, baz)];
// -> this doesn't work, gives an error about "self."
//
// static var to store shared date formatter
static NSDateFormatter *_formatter = nil;
@implementation NSDate (Extras)
// Simple comparison, create a string from today and date, see if they're the same
- (BOOL)isToday
{
// Lazy load and cache formatter, date formatters are slow to init
if (!_formatter)
@zachwaugh
zachwaugh / gist:1264981
Created October 5, 2011 16:50
NSImage category for writing image to file
// [image writeToFile:[NSURL fileURLWithPath:@"/some/path/image.png"]];
- (void)writeToFile:(NSURL *)fileURL
{
NSBitmapImageRep *bitmapRep = nil;
for (NSImageRep *imageRep in [self representations])
{
if ([imageRep isKindOfClass:[NSBitmapImageRep class]])
{
bitmapRep = (NSBitmapImageRep *)imageRep;
@zachwaugh
zachwaugh / gist:1151687
Created August 17, 2011 14:53
Image mask
- (UIImage *)imageWithMask:(UIImage *)maskImage andIsWhite:(BOOL)isWhite
{
CGRect imageRect = CGRectMake(0, 0, maskImage.size.width, maskImage.size.height);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(NULL, maskImage.size.width, maskImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
CGContextClipToMask(ctx, imageRect, maskImage.CGImage);
if (isWhite) {
CGContextSetRGBFillColor(ctx, 1, 1, 1, 1);
} else {
@zachwaugh
zachwaugh / console_test.js
Created February 23, 2011 16:51
Testing node.js console.log output string quoting
console.log('100', 'something');
console.log(100, 'something');
console.log(100, 'something', 'something else');
console.log(100 + ' something');
console.log('something', 100, 'something');
@zachwaugh
zachwaugh / fix-twitter.scpt
Created January 26, 2011 18:14
AppleScript to fix twitter appearing on all spaces issue
tell application "System Events"
set x to application bindings of spaces preferences of expose preferences
set x to {|com.twitter.twitter-mac|:3} & x -- 3 is any space you don't want twitter on
set application bindings of spaces preferences of expose preferences to x
set x to {|com.twitter.twitter-mac|:4} & x -- 4 is the space you want twitter to be on
set application bindings of spaces preferences of expose preferences to x
end tell