Skip to content

Instantly share code, notes, and snippets.

@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 {
import Foundation
// MARK: - Protocol
public protocol Notifier {
associatedtype Notification: RawRepresentable
}
public extension Notifier where Notification.RawValue == String {
//
// KeyboardObserver.swift
// Product
//
// Created by muukii on 3/17/16.
// Copyright © 2016 eure. All rights reserved.
//
import Foundation
import RxSwift
@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 }
}
extension Array {
var powerSet: [[Element]] {
guard !isEmpty else { return [[]] }
return Array(self[1...]).powerSet.flatMap { [$0, [self[0]] + $0] }
}
}
print([1,2,3,4].powerSet) // -> [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3], [4], [1, 4], [2, 4], [1, 2, 4], [3, 4], [1, 3, 4], [2, 3, 4], [1, 2, 3, 4]]
@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
}