Skip to content

Instantly share code, notes, and snippets.

View BeauNouvelle's full-sized avatar
👨‍💻
Working

Beau Nouvelle BeauNouvelle

👨‍💻
Working
View GitHub Profile
extension UIControl {
/// Typealias for UIControl closure.
public typealias UIControlTargetClosure = (UIControl) -> ()
private class UIControlClosureWrapper: NSObject {
let closure: UIControlTargetClosure
init(_ closure: @escaping UIControlTargetClosure) {
self.closure = closure
}
@BeauNouvelle
BeauNouvelle / BigInt.swift
Last active April 22, 2019 13:25
BigInt completed
import UIKit
import Foundation
struct BigInt {
var value: String
func multiply(right: BigInt) -> BigInt {
var leftCharacterArray = value.characters.reversed().map { Int(String($0))! }
var rightCharacterArray = right.value.characters.reversed().map { Int(String($0))! }
extension UITableView {
public func dequeue<T: UITableViewCell>(cellClass: T.Type) -> T? {
return dequeueReusableCell(withIdentifier: cellClass.reuseIdentifier) as? T
}
public func dequeue<T: UITableViewCell>(cellClass: T.Type, forIndexPath indexPath: IndexPath) -> T {
guard let cell = dequeueReusableCell(
withIdentifier: cellClass.reuseIdentifier, for: indexPath) as? T else {
fatalError(
extension UITableView {
public func register<T: UITableViewCell>(cellClass: T.Type) {
register(cellClass, forCellReuseIdentifier: cellClass.reuseIdentifier)
}
}
@BeauNouvelle
BeauNouvelle / UITableViewCell+ReuseIdentifier.swift
Last active April 11, 2019 07:54
UITableViewCell+ReuseIdentifier
extension UITableViewCell {
static var reuseIdentifier: String {
return NSStringFromClass(self)
}
}
let button = UIButton()
button.addAction(for: .touchUpInside) { (button) in
// Run code
}
let slider = UISlider()
slider.addAction(for: .valueChanged) { (slider) in
// Run code
}
@BeauNouvelle
BeauNouvelle / UIBarButtonItem Closure 1.swift
Last active April 11, 2019 02:46
UIBarButtonItem Closure
extension UIBarButtonItem {
/// Typealias for UIBarButtonItem closure.
private typealias UIBarButtonItemTargetClosure = (UIBarButtonItem) -> ()
private class UIBarButtonItemClosureWrapper: NSObject {
let closure: UIBarButtonItemTargetClosure
init(_ closure: @escaping UIBarButtonItemTargetClosure) {
self.closure = closure
}
convenience init(title: String?, style: UIBarButtonItem.Style, closure: @escaping UIBarButtonItemTargetClosure) {
self.init(title: title, style: style, target: nil, action: nil)
targetClosure = closure
action = #selector(UIBarButtonItem.closureAction)
}
@objc func closureAction() {
guard let targetClosure = targetClosure else { return }
targetClosure(self)
}
// Old way of doing things.
let actionButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(someMethod()))
// New way of doing things.
let button = UIBarButtonItem(title: "Done", style: .done) { _ in
// Do stuff here.
}
let view = WorldView(worldSize: 100, cellSize: 5)
view.autoRun()
PlaygroundPage.current.liveView = view