Skip to content

Instantly share code, notes, and snippets.

@loganwright
loganwright / Example.md
Created January 27, 2015 18:29
JSONMappableObject

A basic class for mapping JSON objects to their models. Also fully compliant w/ NSCoding automatically. The only thing you need to do is subclass the model, declare your properties, and then override mapping using the following syntax:

- (NSMutableDictionary *)mapping {
  NSMutableDictionary *mapping = [super mapping];
  mapping[@"propertyName"] = @"associatedJSONKey";
}

If your property maps to another JSONMappableObject, or an array of them, use the following syntax for your mapping key:

@loganwright
loganwright / osxurl.md
Created February 2, 2015 16:57
osx url responder

Add Listener

[[NSAppleEventManager sharedAppleEventManager] setEventHandler:shared
                                                   andSelector:@selector(handleURLEvent:withReplyEvent:)
                                                 forEventClass:kInternetEventClass
                                                    andEventID:kAEGetURL];

Respond to events

@loganwright
loganwright / UIImage+Base64.h
Created February 19, 2015 20:56
UIImage+Base64
#import <UIKit/UIKit.h>
@interface UIImage (Base64)
- (NSString *)base64EncodedVersion;
+ (UIImage *)imageWithBase64String:(NSString *)base64String;
@end
@loganwright
loganwright / UIImage+Resize.h
Created February 19, 2015 20:57
UIImage+Resize
#import <UIKit/UIKit.h>
@interface UIImage (Resize)
- (UIImage *)scaleToSize:(CGSize)size;
@end
@import UIKit;
CGFloat degreesToRadians(CGFloat degrees);
CGFloat distanceBetweenDegrees(CGFloat min, CGFloat max);
CGPoint pointOnArc(CGPoint center, CGFloat radius, CGFloat angleInDegrees);
@loganwright
loganwright / Shell.swift
Last active November 12, 2016 10:46
Call shell commands from Swift
func shell(input: String) -> (output: String, exitCode: Int32) {
let arguments = split(input, maxSplit: Int.max, allowEmptySlices: true) {
$0 == " "
}
let task = NSTask()
task.launchPath = "/usr/bin/env"
task.arguments = arguments
task.environment = [
"LC_ALL" : "en_US.UTF-8",
@loganwright
loganwright / LGOutlinedTextLabel.h
Created March 28, 2015 17:55
Outlined Text Label
#import <UIKit/UIKit.h>
@interface LGOutlinedTextLabel : UILabel
@property (strong, nonatomic) UIColor *outlineColor;
@property (nonatomic) CGFloat outlineWidth;
@end
@loganwright
loganwright / snippets.md
Created March 30, 2015 17:21
Command Line Snippet

Search a directory: http://stackoverflow.com/a/16957078/2611971

Do the following:

grep -rnw 'directory' -e "pattern"

-r is recursive, -n is line number and -w stands match the whole word. Along with these, --exclude or --include parameter could be used for efficient searching. Something like below:

grep --include=\*.{c,h} -rnw 'directory' -e "pattern"
@loganwright
loganwright / After.swift
Created May 21, 2015 18:01
Dispatching events to later in Swift.
func After(after: NSTimeInterval, op: () -> ()) {
After(after, op, nil)
}
func After(after: NSTimeInterval, op: () -> (), completion: (() -> Void)?) {
let seconds = Int64(after * Double(NSEC_PER_SEC))
var dispatchTime = dispatch_time(DISPATCH_TIME_NOW, seconds)
dispatch_after(dispatchTime, dispatch_get_main_queue()) {
let blockOp = NSBlockOperation(block: op)
blockOp.completionBlock = completion
@loganwright
loganwright / Picker.swift
Created June 2, 2015 02:54
Swift Generic UIPickerView
class PickerSource : NSObject, UIPickerViewDelegate, UIPickerViewDataSource {
var data: [[String]] = []
var selectionUpdated: ((component: Int, row: Int) -> Void)?
// MARK: UIPickerViewDataSource
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return data.count
}