Skip to content

Instantly share code, notes, and snippets.

View DependencyContainer.swift
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 Dec 27, 2018
Swift observable protocol
View Observable.swift
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 Dec 19, 2018
Protocol for common ReSwift middleware usecase
View MiddlewareProcessor.swift
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
Pascal to Camel json coding
View PascalJSONCodingTests.swift
import XCTest
struct SampleCodable: Codable {
let sampleVariable1: String
let sampleVariable2: String
let cAPPEDVariable3: String
let sample: String
}
class PascalJSONCoding: XCTestCase {
View Sequence+Walk.swift
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)
}
}
View Sequence+RecursiveFlatten.swift
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)
}
View NavigationSample.swift
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 Sep 7, 2016
Adds strong typed build info to UIApplication
View UIApplication+BuildInfo.swift
import Foundation
extension UIApplication {
var build: String {
return NSBundle.mainBundle().infoDictionary!["CFBundleVersion"] as! String
}
var version: String {
return NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
View excel_to_json.py
#!/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 = []
View NSURLRequestFileInfo.h
#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)