Skip to content

Instantly share code, notes, and snippets.

View perlmunger's full-sized avatar

Matt Long perlmunger

View GitHub Profile
@perlmunger
perlmunger / DropdownButton.h
Created July 7, 2011 22:00
DropdownButton
@interface DropdownButton : UIControl {
}
@property (nonatomic, assign) CAGradientLayer *gradientLayer;
@property (nonatomic, assign) CAGradientLayer *dropdownLayer;
@property (nonatomic, assign) CAShapeLayer *triangleLayer;
@property (nonatomic, retain) UILabel *label;
@perlmunger
perlmunger / NSJSONSerialization_Example.m
Created August 20, 2011 17:38
NSJSONSerialization Example Code
NSError *error = nil;
NSData *json = [NSData dataWithContentsOfFile:path];
id object = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableContainers error:&error];
// object is a dictionary or an array you can now use
@perlmunger
perlmunger / gist:32d8ba0b277ce9bb1618
Last active October 9, 2015 16:22
Safely Set Values For Key On Managed Objects In Swift
extension NSManagedObject {
func safeSetValuesForKeys(dictionary:[NSObject:AnyObject]) {
let attributes: [NSObject:AnyObject] = self.entity.attributesByName
var finalValue : AnyObject? = nil
for (attribute, value) in attributes {
if let val : NSNull = value as? NSNull {
continue;
}
@perlmunger
perlmunger / gist:6653288
Created September 21, 2013 19:10
Add all of the US states to a Rails model from the console with one line
# Here's what it looks like over multiple lines. Use the single line
# one below in the console or it won't work.
# ["AL","AK","AZ","AR","CA","CO","CT","DE","FL","GA","HI","ID","IL",
# "IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT",
# "NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI",
# "SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY","AS","DC",
# "FM","GU","MH","MP","PW","PR","VI",
# "AE","AA","AE","AE","AE","AP"].each { |st| group = Group.new( :name => st); group.save }
["AL","AK","AZ","AR","CA","CO","CT","DE","FL","GA","HI","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY","AS","DC","FM","GU","MH","MP","PW","PR","VI","AE","AA","AE","AE","AE","AP"].each { |st| group = Group.new( :name => st); group.save }
@perlmunger
perlmunger / gist:6827665
Created October 4, 2013 15:17
Write Animated GIF Image with Completion Block
+ (void)writeAnimatedGIFToPath:(NSURL*)path withImages:(NSArray*)images duration:(CGFloat)duration completionBlock:(dispatch_block_t)completionBlock;
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSDictionary *fileProperties = @{
(__bridge id)kCGImagePropertyGIFDictionary: @{
(__bridge id)kCGImagePropertyGIFLoopCount: @0, // 0 means loop forever
}
};
NSDictionary *frameProperties = @{
// ImagePicker encapsulates UIImagePickerViewController functioality providing a convenient
// closure interface for responding to user interactions
import UIKit
import MobileCoreServices
class ImagePicker : NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var didFinishPickingMediaWithInfo:((info:[String:AnyObject]) -> ())?
var didCancelPickingMedia:(() -> ())?
let imagePicker = UIImagePickerController()
@perlmunger
perlmunger / Collections+Additions.swift
Last active April 27, 2016 15:27
Show how to add a mutating function for RangeReplaceableCollectionType that appends items only if they are not already in the collection.
extension RangeReplaceableCollectionType where Generator.Element : Equatable {
mutating func appendDistinct(object : Generator.Element) {
if !self.contains(object) {
self.append(object)
}
}
}
// A derivative solution using filter instead of contains
extension RangeReplaceableCollectionType where Generator.Element : Equatable {
@perlmunger
perlmunger / String+Additions.swift
Last active February 22, 2017 01:06
Some helpful string extensions in Swift 2
extension String
{
var length: Int {
get {
return self.characters.count
}
}
func contains(s: String) -> Bool {
return self.rangeOfString(s) != nil ? true : false
// ISO 8601 Extension for Swift to send dates to Ruby on Rails
extension Date {
init(dateString:String) {
self = Date.iso8601Formatter.date(from: dateString)!
}
static let iso8601Formatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withFullDate,
.withTime,
@perlmunger
perlmunger / logMilestone.swift
Created January 20, 2021 17:11 — forked from atomicbird/logMilestone.swift
Sometimes you just want to print a message that tells you a line of code was executed. Inspired by a tweet from Paige Sun: https://twitter.com/_PaigeSun/status/1161132108875796480
/// Log the current filename and function, with an optional extra message. Call this with no arguments to simply print the current file and function. Log messages will include an Emoji selected from a list in the function, based on the hash of the filename, to make it easier to see which file a message comes from.
/// - Parameter message: Optional message to include
/// - file: Don't use; Swift will fill in the file name
/// - function: Don't use, Swift will fill in the function name
/// - line: Don't use, Swift will fill in the line number
func logMilestone(_ message: String? = nil, file: String = #file, function: String = #function, line: Int = #line) -> Void {
#if DEBUG
// Feel free to change the list of Emojis, but don't make it shorter, because a longer list is better.
let logEmojis = ["😀","😎","😱","😈","👺","👽","👾","🤖","🎃","👍","👁","🧠","🎒","🧤","🐶","🐱","🐭","🐹","🦊","🐻","🐨","🐵","🦄","🦋","🌈","🔥","💥","⭐️","🍉","🥝","🌽","🍔","🍿","🎹","🎁","❤️","🧡","💛","💚","💙","💜","🔔"]
let logEmoji = logEmojis[abs(