Skip to content

Instantly share code, notes, and snippets.

@angelolloqui
Last active February 24, 2019 13:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save angelolloqui/63433ef83551780c4b1085c7a6df40b9 to your computer and use it in GitHub Desktop.
Save angelolloqui/63433ef83551780c4b1085c7a6df40b9 to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import UIKit
////////////
// Nested protocols not compiling
////////////
protocol A {
protocol B {}
}
enum A {
protocol B {}
}
struct A {
protocol B {}
}
class A {
protocol B {}
}
////////////
// Typealiases example 1
////////////
enum Restaurants {
enum List {
typealias ViewModel = RestaurantsListViewModel
}
}
class RestaurantsListViewModel {}
//Usage
let viewModel = Restaurants.List.ViewModel()
////////////
// Typealiases example with protocol
////////////
extension Restaurants.List {
typealias View = RestaurantsListView
}
protocol RestaurantsListView {
init(viewModel: Restaurants.List.ViewModel)
}
class RestaurantsListViewController: UIViewController, Restaurants.List.View {
required init(viewModel: Restaurants.List.ViewModel){
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
//Example usage
let navigationController: UINavigationController? = nil
let vm = Restaurants.List.ViewModel()
//let vc = Restaurants.List.View(viewModel: vm) //If no interface defined
let vc = RestaurantsListViewController(viewModel: vm) //If interface in use, then instantiate particular class or use DI/factorys
navigationController?.pushViewController(vc, animated: true)
////////////
// Module structure example
////////////
//File: search/search.swift
//Full module declaration
enum Search {
enum Filters {}
enum Results {}
}
//File: search/filters/filters.swift
//Filters submodule
extension Search.Filters {
typealias View = NSObject
typealias ViewModel = NSObject
typealias Model = NSObject
}
//File: search/results/results.swift
//Results submodule
extension Search.Results {
typealias View = NSObject
typealias ViewModel = NSObject
typealias Model = NSObject
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment