Skip to content

Instantly share code, notes, and snippets.

View FranDepascuali's full-sized avatar

Depa FranDepascuali

View GitHub Profile
public class MyViewController: UIViewController {
var viewModel: MyViewModel? {
willSet(maybeViewModel) {
if let _ = self.viewModel {
unbindViewModel()
}
}
didSet {
if isViewLoaded(), let viewModel = self.viewModel {
public protocol ViewModelBindable: class {
typealias ViewModel
var viewModel: ViewModel? { get set }
func bindViewModel(viewModel: ViewModel)
func unbindViewModel(viewModel: ViewModel)
extension ViewModelBindable where Self: AnyObject {
public var viewModel: ViewModel? {
// store view model
}
}
extension ViewModelBindable where Self: AnyObject {
public var viewModel: ViewModel? {
get {
return getAssociatedObject(self, key: &AssociatedKey)
}
set(newViewModel) {
if let viewModel = newViewModel {
public final class MyViewController: UIViewController {
// Your implementation
}
extension MyViewController: ViewModelBindable {
public func bindViewModel(viewModel: MyViewModel) {
// binding logic
}
private var AssociatedKey: UInt = 1
private final class AssociatedObjectBox<T> {
let value: T
init(_ x: T) {
value = x
}
}
@FranDepascuali
FranDepascuali / AssociatedObjects.swift
Last active April 7, 2022 18:37
Full implementation
private var AssociatedKey: UInt = 0
private final class AssociatedObjectBox<T> {
let value: T
init(_ x: T) {
value = x
}
}
@FranDepascuali
FranDepascuali / CollapseViews.swift
Last active May 17, 2016 20:59
Collapse & uncollapse views
extension UIView {
private var previousHeightConstraint: NSLayoutConstraint? {
return constraints.filterFirst { $0.firstAttribute == .Height }
}
}
/**
Collapse a view by adding/modifying constraint height.
# https://labs.kunstmaan.be/blog/ios-continuous-delivery-with-jenkins-and-fastlane?this-one-is-for-you-roderik
# Define the minimal Fastlane version
# fastlane_version "1.41.1"
# Use the iOS platform as default
default_platform :ios
# Define what to do for the iOS platform
platform :ios do
input {
syslog {
host => localhost
port => 5000
}
}
filter {
}