Skip to content

Instantly share code, notes, and snippets.

class DependencyContainer {
static let shared = DependencyContainer()
private var types: [String: Any] = [:]
func registerType<T: Any>(_ type: T.Type, named name: String? = nil, creator: @escaping (String) -> T) {
let key = name ?? String(describing: type)
types[key] = creator
}
@pixelrevision
pixelrevision / Observable.swift
Last active December 27, 2018 17:17
Swift observable protocol
import Foundation
public protocol Observable: class {
associatedtype ObservableTarget = AnyObject
func add(observer: ObservableTarget)
func remove(observer: ObservableTarget)
func dispatch(_ handler: (ObservableTarget) -> ())
@pixelrevision
pixelrevision / MiddlewareProcessor.swift
Created December 19, 2018 16:19
Protocol for common ReSwift middleware usecase
import ReSwift
public protocol MiddlewareProcessor {
typealias GetStateFunction = () -> Any?
func preprocess(dispatch: DispatchFunction, action: Action, getState: GetStateFunction)
func postprocess(dispatch: DispatchFunction, action: Action, getState: GetStateFunction)
}
public extension MiddlewareProcessor {
@pixelrevision
pixelrevision / PascalJSONCodingTests.swift
Last active May 17, 2022 16:18
Pascal to Camel json coding
import XCTest
struct SampleCodable: Codable {
let sampleVariable1: String
let sampleVariable2: String
let cAPPEDVariable3: String
let sample: String
}
class PascalJSONCoding: XCTestCase {
extension Sequence {
func walk(_ childSelector: (Element) -> [Element], handler: (Element) -> Void) {
for item in self {
handler(item)
let children = childSelector(item)
if children.count > 0 {
children.walk(childSelector, handler: handler)
}
}
@pixelrevision
pixelrevision / Sequence+RecursiveFlatten.swift
Created December 5, 2017 21:56
Sequence+RecursiveFlatten
extension Sequence {
func recursiveFlatten(_ childSelector: (Element) -> [Element]) -> [Element] {
var output: [Element] = []
for item in self {
output.append(item)
let newItems = childSelector(item)
if newItems.count > 0 {
output += newItems.recursiveFlatten(childSelector)
}
import UIKit
import PlaygroundSupport
class SampleController: UIViewController {
init() {
super.init(nibName: nil, bundle: nil)
title = "Sample View"
view = UIView()
view.backgroundColor = UIColor.green
@pixelrevision
pixelrevision / UIApplication+BuildInfo.swift
Created September 7, 2016 15:13
Adds strong typed build info to UIApplication
import Foundation
extension UIApplication {
var build: String {
return NSBundle.mainBundle().infoDictionary!["CFBundleVersion"] as! String
}
var version: String {
return NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
@pixelrevision
pixelrevision / excel_to_json.py
Created May 24, 2016 19:35
spreadsheet to json
#!/usr/bin/python
import os
import argparse
import xlrd
import json
def create_json_object(args):
workbook = xlrd.open_workbook(args.input)
sheet = workbook.sheet_by_index(0)
json_array = []
#import <Foundation/Foundation.h>
@interface NSURLRequestFileInfo : NSObject
@property NSData *data;
@property NSString *name;
@property NSString *key;
+ (NSURLRequestFileInfo *)fileInfoWithData:(NSData *)data key:(NSString *)key andName:(NSString *)name;
@end
@interface NSURLRequest (FileAdditions)