LoginSignup
94
73

More than 5 years have passed since last update.

KickStarter iOSアプリのStoryboardのenumでの管理のしかたに感動した

Last updated at Posted at 2017-01-16

ふつうの

let storyboard = UIStoryboard(name: "HogeViewController", bundle: nil)
let viewController = storyboard.instantiateInitialViewController() as! HogeViewController

KickStarter-iOSのやりかた

let viewController = Storyboard.Hoge.instantiate(HogeViewController.self)
  • 見やすい
  • 文字列で引っ張る必要がないからtypoしない(補完も出る、コンパイルエラーになる)

実装

ios-oss/Storyboard.swift at master・kickstarter/ios-oss

public enum Storyboard: String {
  case Hoge
  case Fuga
  case ...

  public func instantiate<VC: UIViewController>(_ viewController: VC.Type,
                          inBundle bundle: Bundle = .framework) -> VC {
    // インスタンス化したViewControllerを返してる。
  }
}

enumにメソッド追加できるの凄い・・・

94
73
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
94
73