Instantly share code, notes, and snippets.

Embed
What would you like to do?
//: 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