Skip to content

Instantly share code, notes, and snippets.

View JoshuaSullivan's full-sized avatar

Joshua Sullivan JoshuaSullivan

View GitHub Profile
@JoshuaSullivan
JoshuaSullivan / GenerateRequest.swft
Created May 12, 2015 15:57
Challenge Accepted #2 - Signed API Request
let inputDictionary = [
"author_name": "Robert Jordan",
"book_title": "Knife of Dreams",
"series": "The Wheel of Time, Book 11",
"publisher": "Tor Fantasy",
"published_date": "November 28, 2006"
]
let sortedKeys = inputDictionary.keys.array.sorted(<)
var queryTerms = Array<String>()
@JoshuaSullivan
JoshuaSullivan / LazyInstantiation.swift
Last active August 29, 2015 14:26
This is a Swift 2.0 Playground that demonstrates some use cases for lazy instantiation of object properties.
//: # Lazy Instantiation
//:
//: This playground demonstrates situations where lazily instantiating properties is valuable.
//: **NOTE:** This is a Swift 2.0 playground and must be opened in Xcode 7.
import UIKit
import CoreImage
//: A helper function to produce a random CGFloat in the range 0..<1
func randomCGFloat() -> CGFloat {
@JoshuaSullivan
JoshuaSullivan / ForgotCaptureSemantics.swift
Last active December 24, 2015 22:53
An Exploration of Capture Semantics. Read the blog post: http://www.chibicode.org/?p=28
func attemptLogin(user: String, password: String) {
self.loginRequest = APIClient.sharedClient().createLoginRequest(user:user, password:password) {
result in
switch result {
case .Success(let data):
parseLoginData(data) // Compiler error: implicit reference to self
case .Failure(let error):
errorHandlingMethod(error) // Compiler error: implicit reference to self
}
}
@JoshuaSullivan
JoshuaSullivan / DidSetExample.swift
Last active December 31, 2015 17:48
Swift's didSet property observer is a great way to dynamically configure a view at runtime, but there are limits to what you should do with it. Read the blog post here: http://www.chibicode.org/?p=32
class MyClass {
@IBOutlet weak var outputLabel: UILabel! {
didSet {
// Ensure that the label wasn't just set to nil.
guard let outputLabel = self.outputLabel else { return }
// Set the text color based on the user's style choices.
outputLabel.textColor = StyleManager.sharedManager().outputLabelColor
// Set the label to use fixed-width numbers.
@JoshuaSullivan
JoshuaSullivan / TouchCaptureControl.swift
Created February 18, 2016 16:53
This is a simple control which maps touches within its bounds to a 0.0 - 1.0 range on the horizontal and vertical axes, returned as a CIVector.
class TouchCaptureControl: UIControl {
var value: CIVector = CIVector(x: 0.0, y: 0.0)
@IBInspectable var horizontalMinimumValue: CGFloat = 0.0
@IBInspectable var horizontalMaximumValue: CGFloat = 1.0
@IBInspectable var verticalMinimumValue: CGFloat = 0.0
@IBInspectable var verticalMaximumValue: CGFloat = 1.0
@IBInspectable var flipHorizontalAxis: Bool = false
@IBInspectable var flipVerticalAxis: Bool = false
@JoshuaSullivan
JoshuaSullivan / MakingWaves.pde
Created February 19, 2016 20:49
A Processing 3.0 sketch animating some waves!
class SingleWave {
float SEGMENTS_PER_CYCLE = 48.0;
float wavelength, r, dr;
color drawColor;
SingleWave(float wavelength, color drawColor) {
this.wavelength = wavelength;
this.drawColor = drawColor;
@JoshuaSullivan
JoshuaSullivan / SparkBall.pde
Created February 25, 2016 22:56
A Processing 3 sketch animating sprites moving smoothly about the interior of an imaginary cube.
static float FOCAL_LENGTH = 200.0;
static float ORBIT_RADIUS = 180.0;
static int SPARK_COUNT = 200;
class Spark {
color clr;
float orbitRadius;
float rx, ry, rz, drx, dry, drz;
Spark(color c, float orbitRadius) {
this.clr = c;
@JoshuaSullivan
JoshuaSullivan / OptionalReturn.swift
Created March 3, 2016 16:37
Code snippets for my blog post about functions which return optional values vs functions that throw errors.
import Swift
/// This method returns half of the input value if the number is even. It returns nil if the number is odd.
func evenHalfOrNil(input: Int) -> Int? {
if (input % 2 == 0) {
return input / 2
} else {
return nil
}
}
@JoshuaSullivan
JoshuaSullivan / MTKViewController.swift
Last active April 21, 2016 18:06
Trying to figure out why the View Controller using a MTKView and a Metal-backed CIContext runs at 1/3 of the speed of an GLKView + OpenGLES2-backed CIContext.
//
// ViewController.swift
// MetalCoreImage
//
// Created by Joshua Sullivan on 4/21/16.
// Copyright © 2016 The Nerdery. All rights reserved.
//
import UIKit
import MetalKit
@JoshuaSullivan
JoshuaSullivan / WUndergroundAPIClient.swift
Last active June 23, 2016 15:20
This is a Swift Playground file implementing a solution to Challenge Accepted #49.
//: # Challenge Accepted #49
//: ## WUnderground API Client
//: ### Version 2.0
//:
//: Implement the networking layer for the Umbrella weather app(iOS or Android),
//: using a similar architecture to the one demonstrated in the first episode of Swift Talks.
//:
//: - note:
//: This is my updated solution to the challenge which demonstrates a more powerful and flexible
//: approach to customizing the request sent to the API through the use of closures rather than