Skip to content

Instantly share code, notes, and snippets.

View chriswebb09's full-sized avatar

Christopher Webb chriswebb09

View GitHub Profile
import UIKit
typealias JSON = [String : Any]
fileprivate let imageCache = NSCache<NSString, UIImage>()
extension NSError {
static func generalParsingError(domain: String) -> Error {
return NSError(domain: domain, code: 400, userInfo: [NSLocalizedDescriptionKey : NSLocalizedString("Error retrieving data", comment: "General Parsing Error Description")])
}
}
import UIKit
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
class APIClient {
typealias JSON = [String: Any]
static func search(for query: String, page: Int, completion: @escaping (_ responseObject: JSON?, _ error: Error?) -> Void) {
@chriswebb09
chriswebb09 / linkcrawler.py
Last active April 6, 2017 11:15
Python Requests Example
#!/usr/bin/env python*
# -*- coding: UTF-8 -*-
import requests
import sys
import re
class WebCrawler:
def __init__(self):
@chriswebb09
chriswebb09 / Bank.swift
Last active April 6, 2017 10:18
Bank example
enum Transaction {
case deposit, withdraw
}
class Bank {
typealias Account = (String, Double)
typealias AccountTransaction = (Double, Account) -> Account
static func deposit(amount : Double, account : (name : String, balance : Double)) -> Account {
@chriswebb09
chriswebb09 / APIClient.swift
Created April 18, 2017 04:02
Monitoring Downloads
import UIKit
typealias JSON = [String: Any]
final class iTunesAPIClient: NSObject {
var activeDownloads: [String: Download]?
weak var defaultSession: URLSession? = URLSession(configuration: .default)
// MARK: - Main session used
func testDataStore() {
let dataSource = iTrackDataStore(searchTerm: "new")
let expect = expectation(description: "Data store calls APIClient to access server data and returns iTrack data array.")
dataSource.searchForTracks { tracks, error in
XCTAssert(tracks?.count == 49)
expect.fulfill()
}
import UIKit
typealias TreeNode = TreeElement<Int>
enum Color { case r, b }
indirect enum TreeElement<T: Comparable> {
case empty
case node(Color, TreeElement<T>, T, TreeElement<T>)
import UIKit
func insertionSort(values: [Int]) {
var valuesArray = values
for i in 1...valuesArray.count - 1 {
let nextItem = valuesArray[i]
var currentIndex = i - 1
while currentIndex >= 0 && valuesArray[currentIndex] > nextItem {
valuesArray[currentIndex + 1] = valuesArray[currentIndex]
import UIKit
class ViewController: UIViewController {
let viewOne = UIView()
let viewTwo = UIView()
let intermediaryView = UIView()
let viewSuper = UIView()
override func viewDidLoad() {
@chriswebb09
chriswebb09 / ImageDownloadProtocol.swift
Last active September 3, 2019 22:08
Download on Thread
import UIKit
protocol ImageDownloadProtocol {
func downloadImage(from url: URL, completion: @escaping (UIImage?) -> Void)
}
extension ImageDownloadProtocol {
func downloadImage(from url: URL, completion: @escaping (UIImage?) -> Void) {
let session = URLSession(configuration: .default)
DispatchQueue.global(qos: .background).async {