-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1 added InsantiableViewController protocol
- Loading branch information
Showing
9 changed files
with
96 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
BluetoothExplorer/Controller/Protocols/InstantiableViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// InstantiableViewController.swift | ||
// BluetoothExplorer | ||
// | ||
// Created by Carlos Duclos on 6/26/18. | ||
// Copyright © 2018 PureSwift. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
protocol InstantiableViewController { | ||
|
||
static func instantiate<T>() -> T | ||
} | ||
|
||
extension InstantiableViewController where Self: UIViewController { | ||
|
||
static func instantiate<T>() -> T { | ||
guard let storyboardName = String(describing: self).text(before: "ViewController") else { | ||
fatalError("The controller name is not standard.") | ||
} | ||
|
||
let storyboard = UIStoryboard(name: storyboardName, bundle: Bundle(for: self)) | ||
let identifier = String(describing: T.self) | ||
guard let viewController = storyboard.instantiateViewController(withIdentifier: identifier) as? T else { | ||
fatalError("The storyboard identifier does not exist.") | ||
} | ||
|
||
return viewController | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// String.swift | ||
// BluetoothExplorer | ||
// | ||
// Created by Carlos Duclos on 6/26/18. | ||
// Copyright © 2018 PureSwift. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public extension String { | ||
|
||
func text(before text: String) -> String? { | ||
guard let range = self.range(of: text) else { return nil } | ||
return String(self[self.startIndex..<range.lowerBound]) | ||
} | ||
} |