Skip to content

Instantly share code, notes, and snippets.

View jpavley's full-sized avatar
💭
Working at the office 3 days a week!

John Pavley jpavley

💭
Working at the office 3 days a week!
View GitHub Profile
@jpavley
jpavley / elaspedTime.swift
Created April 17, 2024 17:13
Elapsed time since last logged date function. Returns a string representation of time elapsed since a date.
/// Returns a string representation of the time elapsed since the entry was logged.
/// It works at 4 resolutions:
/// - Full: "5yr, 6mo, 3d, 4h, 25m, 35s"
/// - Years/Months: "5yr, 6mo"
/// - Days/Hours: "3d, 4h"
/// - Minutes/Seconds: "25m, 35s"
/// - Note: Only non-zero values are rendered: "24m" instead of "24m, 0s"
/// - Parameters:
/// - loggedDate: The Date when the record was first logged
/// - full: A flag that specifies the resolution: if true full resolution, if false scoped resolution
@jpavley
jpavley / FuncGen.swift
Created December 16, 2018 20:33
Functions and Generics in Swift 4
import UIKit
var str = "Hello, playground"
/// Int Array Map Function: Adds a value to each element of an array of integers and returns
/// the resulting array
///
/// - Parameters:
/// - delta: value to add to each element of the array
/// - array: array of values to increment
@jpavley
jpavley / CalculatorBrain-Enum-Example
Created October 4, 2015 01:48
CS193P Professor Paul Hegarty iTuneU course
//: Playground - noun: a place where people can play
import UIKit
import Foundation
class CalculatorBrain {
enum Op: CustomStringConvertible {
case Operand(Double)
case BinaryOperation(String, (Double, Double) -> Double)
@jpavley
jpavley / sc193p_example.swift
Last active September 24, 2015 15:47
code from CS193P: Developing iOS 8 Applications with Swift that longer compiles with the solution
// bad code
func performOperationWith(operation: (Double, Double) -> Double) {
if operandStack.count >= 2 {
displayValue = operation(operandStack.removeLast(), operandStack.removeLast())
enter()
}
}
func performOperation(operation: Double -> Double) {
if operandStack.count >= 1 {
@jpavley
jpavley / ios_fonts.m
Last active January 5, 2023 22:47
Iterate through all the fonts families and fonts in an iOS device and print their names to the console.
NSArray *fontFamilyNames = [UIFont familyNames];
for (NSString *familyName in fontFamilyNames) {
NSLog(@"Font Family Name = %@", familyName);
NSArray *names = [UIFont fontNamesForFamilyName:familyName];
NSLog(@"Font Names = %@", names);
}
@jpavley
jpavley / data-source-methods.m
Last active December 19, 2015 01:08
data source methods for app delegates
- (void)awakeFromNib
{
NSLog(@"%@" , NSStringFromSelector(_cmd));
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tv
{
NSLog(@"%@" , NSStringFromSelector(_cmd));
return (NSInteger)0;
}
@jpavley
jpavley / SimpleSpriteIamgeSwap.m
Last active September 24, 2015 22:27
Change a Cocos2D-iPhone sprite's image using a CCSpriteFrame from the CCSpriteFrameCache.
// Assumes you've created a texture atlas with at least 2 image init:
// initial_image.png and new_image.png
// Load the texture atlas into the shared frame cache early in your game…
CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
[frameCache addSpriteFramesWithFile:@"textureAtlas.png"];
// Create your sprite in your layer’s – init method…
// (Note: niceSprite is a CSprint* instance variable so we can reuse it)
niceSprite = [CCSprite spriteWithSpriteFrameName:@"initial_image.png"];