Skip to content

Instantly share code, notes, and snippets.

View justAnotherDev's full-sized avatar

Casey justAnotherDev

  • Scribd
View GitHub Profile
@justAnotherDev
justAnotherDev / ViewPlace.swift
Last active May 11, 2016 10:04
A different approach to laying out views. Designed for Swift, works in ObjC
//
// ViewPlace.swift
//
import UIKit
extension UIView {
/**
Add `self` and `otherView` to a new view and align them along the provided `edge`.
- Returns: A new view that is now the superview of `self` and `otherView`
@justAnotherDev
justAnotherDev / UIGestureRecognizerClosure.swift
Created February 10, 2016 02:33
UIGestureRecognizer + Closure extenstion
extension UIGestureRecognizer {
private class TargetAndSelector: NSObject {
let closure: UIGestureRecognizer -> Void
let recognizer: UIGestureRecognizer
init(recognizer: UIGestureRecognizer, closure: UIGestureRecognizer -> Void) {
self.recognizer = recognizer
self.closure = closure
}
@justAnotherDev
justAnotherDev / GetJSONValue.swift
Created February 3, 2016 10:26
Swift extensions for retrieving values from a JSON response
extension Dictionary where Key: StringLiteralConvertible, Value:AnyObject {
subscript(keysAndIndexes: AnyObject ...) -> AnyObject? {
guard let jsonObject = self as? AnyObject else { fatalError("\(self) is not a json dictionary") }
return _extractValueFrom(keysAndIndexes, from: jsonObject)
}
}
extension Array where Element:AnyObject {
subscript(keysAndIndexes: AnyObject ...) -> AnyObject? {
return _extractValueFrom(keysAndIndexes, from: self)
@justAnotherDev
justAnotherDev / SwiftExceptionHandler.h
Created January 31, 2016 10:30
Swift Exception Handler - tryBlock
NS_INLINE NSException * _Nullable tryBlock(void(^_Nonnull tryBlock)(void)) {
@try {
tryBlock();
}
@catch (NSException *exception) {
return exception;
}
return nil;
}
@justAnotherDev
justAnotherDev / DictionaryOfInstanceVariables.swift
Created January 31, 2016 09:47
DictionaryOfInstanceVariables - Swift version of NSDictionaryOfVariableBindings
func DictionaryOfInstanceVariables(container:AnyObject, objects: String ...) -> [String:AnyObject] {
var views = [String:AnyObject]()
for objectName in objects {
guard let object = object_getIvar(container, class_getInstanceVariable(container.dynamicType, objectName)) else {
assertionFailure("\(objectName) is not an ivar of: \(container)");
continue
}
views[objectName] = object
}
return views
@justAnotherDev
justAnotherDev / iterate.swift
Created January 5, 2016 20:30
Iterate a tuple in Swift
func generatorForTuple(tuple: Any) -> AnyGenerator<Any> {
return anyGenerator(Mirror(reflecting: tuple).children.lazy.map { $0.value }.generate())
}
let someTuple = ("1", 2, ["3"])
for value in generatorForTuple(someTuple) {
print(value)
}
@justAnotherDev
justAnotherDev / SwiftRequests.swift
Last active May 11, 2016 10:06
First stab at a Swift network requestor
//
// SwiftRequests.swift
//
import Foundation
@objc class SwiftRequests : NSObject {
enum SwiftRequestsError {
case InvalidURL
-(NSArray *)allInstanceVariablesForClass:(Class)klass {
NSMutableArray *iVarList = [NSMutableArray array];
unsigned int count;
Ivar* ivars=class_copyIvarList(klass, &count);
for(int i=0; i < count; i++) {
Ivar ivar= ivars[i];
@justAnotherDev
justAnotherDev / gist:aee9b759f932bc89df59
Last active August 29, 2015 14:25
Python SCP/SSH Commands
#!/usr/bin/python2.4
import string
from string import rstrip
import subprocess
from subprocess import Popen, PIPE, call
from time import time
import os
# copy a folder to a remote folder