Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Injecting data to view controller Swift 4
// Inspired by
extension UIStoryboard {
func instantiateViewController<T: UIViewController>(_ type: T.Type) -> T {
return instantiateViewController(withIdentifier: type.classIdentifier) as! T
extension UIViewController {
static var classIdentifier: String { return String(describing: self) }
class SearchDialogViewController: UIViewController {
var data: [Searchable]!
static func createWith(storyboard: UIStoryboard = R.storyboard.entryFlow(), data: [Searchable]) -> Self {
return storyboard.instantiateViewController(self).then { vc in = data

This comment has been minimized.

Copy link

@icanzilb icanzilb commented Dec 14, 2017

you need to set your data as private so that the only place to change the value is from within the class - e.g. no other code can randomly change it from outside 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment