Skip to content

Instantly share code, notes, and snippets.

View JoeFerrucci's full-sized avatar

Joe Ferrucci JoeFerrucci

  • Instacart
  • San Francisco, CA
View GitHub Profile
@JoeFerrucci
JoeFerrucci / String+Additions.swift
Last active April 16, 2016 01:17
Repeat a String a number of times.
extension String {
func times(count: Int, separatedBy separator: String = "") -> String {
return Array(count: count, repeatedValue: self).joinWithSeparator(separator)
}
static func times(count: Int, repeatedValue s: String, separatedBy separator: String = "") -> String {
return Array(count: count, repeatedValue: s).joinWithSeparator(separator)
}
}
API CODE:
===========
GET api/:id/changes
route.get("api/:id/changes", function() {
queryMongoDB(USER_CHANGES).filter("id == %@", id, function(changesData) {
queryMongoDB()
@JoeFerrucci
JoeFerrucci / zipmap.swift
Created May 2, 2016 21:04
Threw this together real quick. I'll refactor for more performant code and structure. Performance profiling coming soon.
import Foundation
extension Array {
func zipmap(key key: String, value: String, container: [AnyObject]) -> [[String:AnyObject]] {
var result = [[String:AnyObject]]()
let count = min(self.count, container.count)
for i in 0..<count {
var mapping = [String:AnyObject]()
let k = self[i] as! String
@JoeFerrucci
JoeFerrucci / protocols.md
Created May 14, 2016 07:00 — forked from rbobbins/protocols.md
Notes from "Protocol-Oriented Programming in Swift"

PS: If you liked this talk or like this concept, let's chat about iOS development at Stitch Fix! #shamelessplug

Protocol-Oriented Programming in Swift

Speaker: David Abrahams. (Tech lead for Swift standard library)

  • "Crusty" is an old-school programmer who doesn't trust IDE's, debuggers, programming fads. He's cynical, grumpy.

  • OOP has been around since the 1970's. It's not actually new.

  • Classes are Awesome

    • Encapsulation
    • Access control
@JoeFerrucci
JoeFerrucci / JFTextStyle.swift
Created May 16, 2016 23:36
A functional wrapper around NSMutableAttributedString
/*
let myName = NSMutableAttributedString(string: " Hey, what's up!? ")
let yellowOnBlack = Styles.textBackground(.redColor(), range: NSMakeRange(2, 3)) >>> Styles.textColor(.darkGrayColor())
let label = UILabel(frame: CGRectMake(10, 50, 200, 50))
label.attributedText = yellowOnBlack(myName)
view.addSubview(label)
*/
import UIKit
import Foundation
@JoeFerrucci
JoeFerrucci / CopyableLabel.swift
Created May 20, 2016 17:04
CopyableLabel - a typical UILabel with "Copy" functionality.
import UIKit
class CopyableLabel: UILabel {
override init(frame: CGRect) {
super.init(frame: frame)
userInteractionEnabled = true
addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(CopyableLabel.showMenu(_:))))
}
@JoeFerrucci
JoeFerrucci / gist:31f76ad0c22d1b43bcd968f2dcd46780
Created July 21, 2016 00:44
map a range of values to a different range of values.
private func mapValueToRange(value: CGFloat, largerRangeMin: CGFloat, largerRangeMax: CGFloat, targetRangeMin: CGFloat, targetRangeMax: CGFloat) -> CGFloat {
let largerSpan = largerRangeMax - largerRangeMin
let targetSpan = targetRangeMax - targetRangeMin
// Scaled to 0-1 range.
var scaleBy = (value - largerRangeMin) / largerSpan
if scaleBy < 0 { scaleBy = 0 }
if scaleBy > 1 { scaleBy = 1 }
@JoeFerrucci
JoeFerrucci / tableViewContentHeight.swift
Last active July 21, 2016 23:07
Finding the height of your UITableView's current content.
/////////////////////////////////////////////////////////////////////
// finding the height of your UITableView's current content:
/////////////////////////////////////////////////////////////////////
let tableView = UITableView()
func contentHeight(forTableView tableView: UITableView) -> CGFloat {
let lastSectionIndex = tableView.numberOfSections - 1
let lastRowIndex = tableView.numberOfRowsInSection(lastSectionIndex)
@JoeFerrucci
JoeFerrucci / UIColorHex.swift
Created July 27, 2016 21:04
Easy UIColors using hex values, no more slower string manipulation.
extension UIColor {
convenience init(hex: Int) {
self.init(red: CGFloat((hex & 0xFF0000) >> 16) / 255.0,
green: CGFloat((hex & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(hex & 0x0000FF) / 255.0,
alpha: 1.0)
}
}

Videos