Skip to content

Instantly share code, notes, and snippets.

View robtimp's full-sized avatar

Rob Hudson robtimp

  • Apple
  • Seymour, CT
View GitHub Profile
@robtimp
robtimp / smartquotes.swift
Created May 11, 2015 22:11
Swift smart quotes
// Light-weight Swift smart quote implementation
// By Rob Hudson
// 5/11/2015
import UIKit
let text = "\"Would you tell me, please, which way I ought to go from here?\" " +
"That depends a good deal on where you want to get to,\" said the Cat. " +
"\"I don’t much care where-\" said Alice. " +
"Then it doesn’t matter which way you go,\" said the Cat. " +
@robtimp
robtimp / stringparsing.swift
Last active August 29, 2015 14:21
String Parsing (objc.io #9) Swift version
// objc.io Issue #9
// February, 2014
// String Parsing by Chris Eidhof
//
// Swift version by Rob Hudson
import UIKit
// NSTextCheckingResult using NSRange, rather than Range<String.Index>, so we need an NSString, rather than a String
let string: NSString = "backgroundColor = #ff0000"
@robtimp
robtimp / stringparsing2.swift
Created May 21, 2015 19:14
String Parsing (objc.io #9) in Swift - Example 2
// objc.io Issue #9
// February, 2014
// String Parsing by Chris Eidhof
//
// Example 2
// Swift version by Rob Hudson
import UIKit
let FormatError = 100
@robtimp
robtimp / stringparsing3.swift
Created May 21, 2015 20:01
String Parsing (objc.io #9) example 3
// objc.io Issue #9
// February, 2014
// String Parsing by Chris Eidhof
//
// Example 3
// Swift version by Rob Hudson
import UIKit
let FormatError = 100
func sendEmail(email: Email, completion: SendEmailCompletionBlock) -> Request {
let fields: JSONDict = [
"first_name" : email.firstName,
"last_name" : email.lastName,
"email_address" : email.emailAddress,
"ip_address" : email.ipAddress,
"vendor_id" : email.vendorID,
"event_date_epoch" : email.eventDateEpoch,
"message" : email.message,
"phone" : email.phoneNumber
@robtimp
robtimp / NoteSpeller.swift
Last active May 4, 2018 23:06
Updated for Swift 4
struct NoteSpeller {
static let wordURL = "https://raw.githubusercontent.com/dwyl/english-words/master/words.txt"
static let delimiter: Character = "\n"
static let notesNames: [Character] = ["a", "b", "c", "d", "e", "f", "g"]
static func getWords(includeH: Bool = false) -> [String] {
var notes = notesNames
if includeH {
notes.append("h")
import Foundation
struct Random {
static func randomNumberUpTo(upTo: Int) -> Int {
return Int(arc4random_uniform(UInt32(upTo)))
}
static func randomNumberInRange(range: Range<Int>) -> Int {
let distance = range.endIndex - range.startIndex
let random = randomNumberUpTo(distance)
func ordinal(forNumber: Int) -> String {
let tens = (number / 10) % 10
let ones = number % 10
let suffix: String
switch (tens, ones) {
case (1, _):
suffix = "th"
case (_, 1):
@robtimp
robtimp / LastDayOfMonth.swift
Last active June 8, 2017 18:56
Last day of the month
// Returns 30 or 31 (except for February)
NSCalendar.currentCalendar().rangeOfUnit(.Day, inUnit: .Month, forDate: NSDate()).length
@robtimp
robtimp / ListNode.swift
Last active April 10, 2018 20:39
ListNode with description
public class ListNode: ExpressibleByArrayLiteral {
public var value: Int
public var next: ListNode?
public init(_ value: Int, next: ListNode? = nil) {
self.value = value
self.next = next
}
public required init(arrayLiteral elements: Int...) {
self.value = elements[0]