0

Example: There is a KingDetailViewController class with the following code.

    // MARK: - New Instance
extension KingDetailViewController {
    class func newInstance() -> KingDetailViewController {
        return Storyboard
            .main
            .instantiateViewController(withIdentifier: String
                .init(describing: KingDetailViewController.self)) as! KingDetailViewController
    }
}

Is this a correct approach?

  • String.init(describing: KingDetailViewController.self) This is bound to blow up. – Alexander May 8 '18 at 16:25
  • @Alexander - Thanks for quick comments, but the question was around the best practice. – UIResponder May 9 '18 at 7:32
  • I know, hence why my comment is a comment and not an answer – Alexander May 9 '18 at 15:52
  • 1
    What happens if you move this class into a different module, giving it a difference prefix? SomeModule.KingDetailViewController – Alexander May 10 '18 at 16:22
  • 1
    What exactly are you trying to accomplish? Are you asking how best to instantiate something from a storyboard, or where to put the code for a newInstance method? If it is the latter, an extension is a fine place, but best practice would be to use a (class) computed property instead of a method. For example, UserDefaults.standard – JSquared Jun 29 '18 at 15:20

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.