LoginSignup
6
4

More than 5 years have passed since last update.

StoryboardのCustomClassにInnerClassを指定

Last updated at Posted at 2017-10-17

swiftにはmodule単位でしかnamespaceの概念がないので、インナークラスで乗り切りたくなることがよくある。
Storyboardで指定するクラスをインナークラスにするためには工夫がいるのでその手順をメモ

環境

  • xcode 9.0
  • swift4

手順

ソースコードの調整

Staff.swift
class Staff {
  // namespace用なので中身はなし
}
StaffViewController.swift
// RunPhaseのCompileSouresで必ずStaff.swiftより後になるように設定すること

extension Staff {

  // 括弧の中はstoryboardから指定するときの名前を入れる(なんでも良い)
  @objc(StaffViewController)
  class ViewController: UIViewController {
    // 何かする
  }
}

Storyboard上の操作

UIViewController > CustomClass > Class、無理やり「StaffViewController」と指定する。
保管も出ないし、「UIViewController > CustomClass > Module」がNoneに変わるがそれで良い。(「Inherit Module From Target」に戻すとUnknown Classで落ちる)


投稿してから気づいた。
ViewControllerのときはうまくいくけれども、Viewのときはダメみたい…。
が、そもそもViewのサブクラスに「namespaceで区切りたいようなドメイン固有のロジック」をもたせたいとおもわないので不便はなかったのである。

6
4
0

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
6
4