Skip to content

Instantly share code, notes, and snippets.

@vhart
vhart / gist:1a3ad4942f495854f33b
Last active August 29, 2015 14:22
12 Days of Christmas
//
// main.m
// 12DaysOfChristmas
//
// Created by Varindra Hart on 6/4/15.
// Copyright (c) 2015 Varindra Hart. All rights reserved.
//
#import <Foundation/Foundation.h>
//
// main.m
// variable2
//
// Created by Varindra Hart on 6/4/15.
// Copyright (c) 2015 Varindra Hart. All rights reserved.
//
#import <Foundation/Foundation.h>
@vhart
vhart / UIColorFromHex.swift
Last active January 6, 2017 03:00
UIColor from hex
extension UIColor {
convenience init? (fromHex hex: String) {
let hexPattern = try! NSRegularExpression(pattern: "^[0-9a-fA-F]{6}$",
options: [.anchorsMatchLines])
let range = NSRange(location: 0, length: hex.characters.count)
guard hexPattern.matches(in: hex,
options: [],
range: range).count == 1
else { return nil }
@vhart
vhart / ProtocolGenericsPt1.swift
Last active November 28, 2016 17:16
Protocol-Generics Pt 1
protocol Drivable {
func drive()
var numberOfWheels: Int { get }
}
func startTraveling(with transportation: Drivable) { }
func startTraveling<D: Drivable>(with transportation: D) { }
@vhart
vhart / ProtocolGenericsPt2.swift
Last active November 28, 2016 17:16
ProtocolGenericPt2
struct Car: Drivable {
let numberOfWheels = 4
func drive() { }
}
struct Motorcycle: Drivable {
let numberOfWheels = 2
let licensePlate = "asdf123"
@vhart
vhart / StringLayoutHandler.swift
Last active January 10, 2017 18:58
StringLayoutHandler
import UIKit
import Foundation
struct StringLayoutHandler {
enum SerializationTokens: String {
case tab = "T"
case newline = "N"
var token: String { return "#" + rawValue }
@vhart
vhart / BasicLogger.swift
Last active October 16, 2017 05:56
Logger
class BasicLogger: NSObject {
private static let queue = dispatch_queue_create("com.vhart.BasicLoggerQueue", DISPATCH_QUEUE_SERIAL)
private static let dateFormatter = NSDateFormatter()
static var filePath: String {
return fileUrl.path!
}
static var fileUrl: NSURL {
@vhart
vhart / Swift3BasicLogger.swift
Last active April 17, 2018 19:21
Basic Logger - Swift 3 Version
/*
from console. `po NSHomeDirectory()`
from terminal cd to that path
cd into Documents
LogFile.txt should be there
To see content update live run tail -F LogFile.txt
*/
public class BasicLogger: NSObject {
@vhart
vhart / TEParserProtocol.swift
Last active August 28, 2017 19:08
Type Erasure Protocol
enum Result<T, E: Error> {
case success(T)
case failure(E)
}
enum NetworkError: Error {
case invalidJson
case badRequest
case timeout
}
@vhart
vhart / TEParserProtocol2.swift
Created August 28, 2017 15:03
Show error that results in wrapping abstract protocol with associated type requirements
class DeserializerBox<D: JsonDeserializer> {
let deserializer: D
init(deserializer: D) {
self.deserializer = deserializer
}
}
class Session<P>: NetworkSession {
typealias Payload = P