Skip to content

Instantly share code, notes, and snippets.

@brocoo
brocoo / PaginatedList.swift
Created April 19, 2018 10:31
Generic data source storing pages of content, providing an efficient way to handle pagination.
import Foundation
// MARK: -
public struct PaginatedList<T: Decodable> {
// MARK: - Action
enum Mutation {
case created
@brocoo
brocoo / ReactiveExtensions.swift
Created December 21, 2016 15:47
Reactive extensions for RxSwift
import RxCocoa
import RxSwift
// MARK: - Reactive protocol
protocol ReactiveDisposable {
var disposeBag: DisposeBag { get }
}
@brocoo
brocoo / ReactiveExtensions.swift
Last active December 1, 2016 18:06
Reactive extension on UIViewController, adding observables for the basic UIViewController life cycle functions
import RxSwift
import RxCocoa
extension Reactive where Base: UIViewController {
public var viewDidLoad: Observable<Void> {
return self.sentMessage(#selector(UIViewController.viewDidLoad)).map({ _ in return () })
}
public var viewWillAppear: Observable<Bool> {
@brocoo
brocoo / AlertView.swift
Created November 14, 2016 11:49
A customisable alert view class to replace the default UIAlertController (Swift3)
public typealias AlertViewClosure = (AlertView) -> Void
// MARK: -
public final class AlertView: UIView {
// MARK: - AlertViewButton
public struct Button {
@brocoo
brocoo / Fonts.swift
Last active September 28, 2016 15:41
Print a list available of fonts
let fontFamilyNames = UIFont.familyNames
for familyName in fontFamilyNames {
print("------------------------------")
print("Font Family Name = [\(familyName)]")
let names = UIFont.fontNames(forFamilyName: familyName)
print("Font Names = [\(names)]")
}
@brocoo
brocoo / CollectionType.swift
Created August 19, 2015 15:41
Add a firstMatching function for CollectionType
extension CollectionType {
/// Returns the first element where `predicate` returns `true` for the
/// corresponding value, or `nil` if such value is not found.
///
/// - Complexity: O(`self.count`).
func firstMatching(@noescape predicate: (Self.Generator.Element) -> Bool) -> Self.Generator.Element? {
for (_, element) in self.enumerate() {
if predicate(element) { return element }
}
@brocoo
brocoo / Extensions.swift
Created July 23, 2015 11:34
Perform closure once for a given key
// MARK: - NSUserDefaults
extension NSUserDefaults {
class func performOnceForKey(key: String, perform: () -> Void, elsePerform: (() -> Void)? = nil) {
let once = self.standardUserDefaults().objectForKey(key)
self.standardUserDefaults().setBool(true, forKey: key)
self.standardUserDefaults().synchronize()
if once == nil { perform() }
@brocoo
brocoo / Point.swift
Created July 22, 2015 22:19
CGPoint extension for SpriteKit
extension CGPoint {
public enum CoordinateSystem {
case UIKit
case SpriteKit
}
public func coordinates(from from: CoordinateSystem, to: CoordinateSystem) -> CGPoint {
if from == to { return self }
else {
@brocoo
brocoo / Settings.swift
Created July 22, 2015 20:24
Settings struct
struct Settings {
// Make sure to add the "-D DEBUG" flag in the project settings for the Swift Compiler
static var Debug: Bool {
#if DEBUG
return true
#else
return false
#endif
}
@brocoo
brocoo / Flip.swift
Last active August 29, 2015 14:25
Flip UIView around any axis
extension CGFloat {
var toRadians: CGFloat {
return self * CGFloat(M_PI) / 180.0
}
var toDegress: CGFloat {
return self * 180 / CGFloat(M_PI)
}
}