Skip to content

Instantly share code, notes, and snippets.

@patricklynch
patricklynch / UIView+find.swift
Created November 7, 2017 11:34
Subview recursive search
extension UIView {
func findSubview(where matches: (UIView) -> Bool) -> UIView? {
for subview in subviews {
if matches(subview) {
return subview
} else if let match = subview.findSubview(where: matches) {
return match
class User {
static let URL_SAVE_TEAM = URL(string: "http://localhost/test/login_mobile.php")!
func login(email: String, password: String, completion: @escaping (String?)->()) {
var request = URLRequest(url: User.URL_SAVE_TEAM)
request.httpMethod = "POST"
let postParameters = "email=\(email)&password=\(password)"
import Foudnation
extension Dictionary where Key: AnyHashable, Value: AnyHashable {
var queryString: String? {
var output: String = ""
var i = 0
for (key, i) in self.keys.enumerate() {
output += prefix + "\(key)=\(self[value])"
}
@patricklynch
patricklynch / Operation.swft
Last active August 29, 2015 14:27
NSOperation Subclass
/// An NSOperation subclass with utilities for creating long-running operations or
/// operations that must pause and wait for some result, as well as a concise API
/// to make them easy to create, configure and add to background and main queues.
class Operation: NSOperation {
/// Shared background queue
static let backgroundQueue: NSOperationQueue = {
var queue = NSOperationQueue()
queue.maxConcurrentOperationCount = 5
return queue
@patricklynch
patricklynch / SetUtils.swift
Last active August 29, 2015 14:26
CoreData Set Utils
/// Some extensions with overloaded operators for easily adding and removing
/// objects to sets when working with CoreData
import Foundation
import CoreData
protocol ManagedSet {
var allObjects: [AnyObject] { get }
init( array: [AnyObject] )
}
@patricklynch
patricklynch / GCDShorthand.swift
Last active August 11, 2016 02:24
GCD Shorthand
// Some short hand GCD to keep code cleaner
func dispatch_after( delay:Double, closure:()->() ) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
@patricklynch
patricklynch / gist:8d5b792c5fa2aad95fe7
Last active August 29, 2015 14:24
Using a switch case to unwrap optionals when parsing JSON data (Swift 1.2)
import Foundation
func parse( data: NSData ) {
var error: NSError? = nil
switch NSJSONSerialization.JSONObjectWithData( data, options: nil, error: &error ) {
case .Some(let dictionary as NSDictionary):
println( "JSON contained dictionary: \(dictionary)" )
case .Some(let array as NSArray):
println( "JSON Contained array: \(array)" )
default:
@patricklynch
patricklynch / gist:a6babe376dd959e60b97
Last active August 29, 2015 14:24
Random Generator
// Generates various types of random values
//
// Created by Patrick Lynch on 6/30/15.
// Copyright © 2015 Patrick Lynch. All rights reserved.
import Foundation
struct Random {
// Designed for you to swap out for any other core random function
@patricklynch
patricklynch / gist:689525d466c4ada42b8e
Last active October 30, 2021 05:23
The 6 ways to unwrap optionals in Swift 2.0
// The 6 ways to uwnrap optionals in Swift 2.0
//
// Created by Patrick Lynch on 6/28/15.
// Copyright © 2015 Patrick Lynch. All rights reserved.
import Foundation
// 1. Force Unwrapping
// 2. Optional Binding
// 3. Optional Chaining
@patricklynch
patricklynch / gist:0ab26319bd46afd33886
Last active August 29, 2015 14:23
Responder with generics
// A quick way to set up use of responder chain with a bit more type safety
//
// Created by Patrick Lynch on 6/28/15.
// Copyright © 2015 Patrick Lynch. All rights reserved.
import UIKit
protocol Responder {
func targetForAction<T, U: RawRepresentable where U.RawValue == Selector>( action: U, withSender sender: AnyObject?) -> T
}