質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

3541閲覧

required init?(coder aDecoder: NSCoder) が呼ばれる理由について

kjfnfljnf

総合スコア23

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

1クリップ

投稿2020/04/11 23:13

Swift: Version 5.1.3
XCode: Version 11.3.1

こんにちは。

このプロジェクトを参考にしてアプリを作っています。

その中にこのようなコードがあります。

swift

1/// Manages flow of screens in the app 2final class AppFlowController: UIViewController, AlertShowing { 3 fileprivate let drinks: [Drink] 4 5 // MARK: - Init 6 7 init(drinks: [Drink]) { 8 self.drinks = drinks 9 super.init(nibName: nil, bundle: nil) 10 } 11 12 required init?(coder aDecoder: NSCoder) { 13 fatalError("OrderFlowController must be initialized using `init(drinks:)`") 14 } 15 1617}

AppDelegate内で下記のコードでVCを読んでいます。

swift

1 private lazy var appFlowController = AppFlowController(drinks: DrinkMenu.all)

ブレイクポイントを使って確認すると,init(drinks: [Drink])

はしっかり呼ばれており,drinksにもオブジェクトが入っていることは確認できました。

にもかかわらず,required init?でエラーが出るのは一体何故なのでしょうか。

よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

init?(coder:) は xib や Storyboard から ViewController が生成されるときに呼ばれるメソッドです。たぶん Main.storyboard から ViewController が生成されてると思うので、これを無効にする必要があります。

で、Xcode 11 (iOS 13) では SceneDelegate というのが追加されてるので、以下の修正でいけると思います。

  1. Info.plist の Main storyboard file base name を削除。

  2. Info.plist の Application Scene Manifest の中にある Storyboard Name を削除。

イメージ説明

  1. appFlowController の初期化コードは AppDelegate ではなく SceneDelegate に記述。

diff

1 class SceneDelegate: UIResponder, UIWindowSceneDelegate { 2 3 var window: UIWindow? 4- 5+ private lazy var appFlowController = AppFlowController(drinks: DrinkMenu.all) 6 7 func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { 8 // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. 9 // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. 10 // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). 11 guard let _ = (scene as? UIWindowScene) else { return } 12+ 13+ if let windowScene = scene as? UIWindowScene { 14+ let window = UIWindow(windowScene: windowScene) 15+ window.rootViewController = appFlowController 16+ self.window = window 17+ window.makeKeyAndVisible() 18+ } 19 }

参考: Setup SceneDelegate Without Storyboard | @samwize

投稿2020/04/12 09:00

hoshi-takanori

総合スコア7895

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kjfnfljnf

2020/04/13 04:36

おっしゃる通りでした。 まず,Storyboardとコードのコネクションを切った時点で,エラーが消えました。 SceneDelegateも見落としていました。ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問