Skip to content

Instantly share code, notes, and snippets.

View IanKeen's full-sized avatar
🏂

Ian Keen IanKeen

🏂
View GitHub Profile
@IanKeen
IanKeen / Example.swift
Last active July 10, 2019 22:02
Small utility methods to simplify dealing with Reusable items i.e. table/collection view cells
//`UITableViewCell` and `UICollectionViewCell` are `Reusable` by defaut
//Use the extension method to dequeue an instance of the appropriate `Reusable`
class MyVC: UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
tableView
.registerReusable(FooCell.self)
.registerReusable(BarCell.self)
}
@IanKeen
IanKeen / IKScrollView.swift
Last active November 18, 2021 16:34
A UIScrollView subclass that allows you to use scrollviews and autolayout without the drama...
enum SizeMatching {
case Width, Height, Both, None
}
class IKScrollView: UIScrollView {
//MARK: - Outlets
@IBOutlet private var contentView: UIView?
//MARK: - Properties
@IBInspectable var sizeMatching = SizeMatching.Width

New issue checklist

Before submitting this issue, make sure you have done the following:

Bug report

@IanKeen
IanKeen / NibLoadableView.swift
Last active May 9, 2018 17:30
Class to allow you to use NIB BASED custom UIView in interface builder
import UIKit
open class NibLoadableView: UIView {
//MARK: - Public Properties
private(set) var customView: UIView!
//MARK: - Lifecycle
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.nibSetup()
@IanKeen
IanKeen / NSTimeInterval+Extension.swift
Last active October 16, 2019 04:32
Working Swiftly with NSTimeInterval
enum TimePeriod {
case Seconds(Int)
case Minutes(Int)
case Hours(Int)
var timeInterval: NSTimeInterval {
switch self {
case .Seconds(let value): return NSTimeInterval(value)
case .Minutes(let value): return NSTimeInterval(value * 60)
case .Hours(let value): return NSTimeInterval(value * 60 * 60)
@IanKeen
IanKeen / UIControl.swift
Created April 3, 2016 21:04
UIControl.swift - Adds a closure based target/action support to all UIControl based components, including basic lifecycle handling
public protocol ClosureActionable: class {
func addTarget(target: AnyObject, forControlEvents controlEvents: UIControlEvents, closure: (Self) -> Void) -> AnyObject
func removeTarget(pointer: AnyObject)
}
extension UIControl: ClosureActionable { }
public extension ClosureActionable where Self: UIControl {
private var pointers: [TargetPointer] {
get {
if let existing = objc_getAssociatedObject(self, ClosureAssociatedKeys.Pointers) as? [TargetPointer] { return existing }
@IanKeen
IanKeen / Dictionary+Plist.swift
Created April 4, 2016 19:27
Reading plists into a typed Dictionary: Snippet 5
extension Dictionary {
init(plistNamed fileName: String, bundle: NSBundle = NSBundle.mainBundle()) {
guard
let pList = bundle.pathForResource(fileName, ofType: "plist"),
let dict = NSDictionary(contentsOfFile: pList)
else { self = [:]; return }
var result = [Key: Value]()
dict.forEach { key, value in
if let key = key as? Key, let value = value as? Value {
@IanKeen
IanKeen / ObservableType+Weak.swift
Created April 6, 2016 18:36
Allow a more concise way to weakly subscribe to Observables using self.someFunction
//
// ObservableType+Weak.swift
//
// Created by Ian Keen on 6/04/2016.
// Copyright © 2016 Ian Keen. All rights reserved.
//
import Foundation
import RxSwift
@IanKeen
IanKeen / KeyPathObserver.swift
Last active February 24, 2017 01:15
Swifty KVO - Type-safe access, no 'stringly-typed' garbage here ;)
public protocol KeyPathCollection: RawRepresentable, Hashable {
static var allKeyPaths: [Self] { get }
}
public extension KeyPathCollection where RawValue: Hashable {
var hashValue: Int { return self.rawValue.hashValue }
}
class KeyPathBox {
weak var object: AnyObject?
var closure: () -> Void
@IanKeen
IanKeen / UIView+move.swift
Created July 26, 2016 21:47
UIView.move(_:to) concept - a thought about a what a 'swifty' api might look like
public enum ViewIndex {
case front, back
case index(Int)
case above(UIView)
case below(UIView)
}
private extension ViewIndex {
private func indexOfView(view: UIView, in parent: UIView) throws -> Int {
guard let index = parent.subviews.index(of: view) else { throw ViewIndexError.viewNotInHierarchy(view) }
return index