Home>

I tried to create a youtube timeline layout using the collection view.
Error text libc ++ abi.dylib: terminating with uncaught exception of type NSException
Has come out.

I do not use storyboards and aim to implement code only.
I was imitating this video.
Reference video

I'd like to know how to deal with this error.
Thank you.

Error message
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: '-[UICollectionViewController loadView] instantiated view controller with identifier "UIViewController-BYZ-38-t0r" from storyboard "Main", but didn't get a UICollectionView.'
*** First throw call stack:
Modified source code
@ UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func application (_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?)->Bool {
        window = UIWindow (frame: UIScreen.main.bounds)
        window? .makeKeyAndVisible ()
        let layout = UICollectionViewFlowLayout ()
        window? .rootViewController = UINavigationController (rootViewController: HomeController (collectionViewLayout: layout))
        return true
    }
class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    override func viewDidLoad () {
        super.viewDidLoad ()
        navigationItem.title = "Home"
        collectionView? .backgroundColor = UIColor.white
        collectionView? .register (UICollectionViewCell.self, forCellWithReuseIdentifier: "cellId")
    }
    override func collectionView (_ collectionView: UICollectionView, numberOfItemsInSection section: Int)->Int {
        return 5
    }
    override func collectionView (_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)->UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell (withReuseIdentifier: "cellId", for: indexPath)
        cell.backgroundColor = UIColor.blue
        return cell
    }
    func collectionView (_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath)->CGSize {
        return CGSize (width: view.frame.width, height: 200)
    }
}

I think this is an application of this question.
However, I didn't know how to solve it in my case, so I asked a question.
Reference link

The storyboard's ViewController class is HomeController.
I think that tying is not a problem.

Supplemental information

Xcode Version 11.1
Swift 5.1
I use UIKit

Thank you for the long sentence.
I am very sorry for myself, but I would appreciate it if anyone could help.

  • Answer # 1

    SceneDelegate has been used since Xcode11, and the screen configuration has changed.

    https://qiita.com/edasan/items/68cbe9ab63d48ee71594

    I think this article will be helpful.

    To generate a screen without using Storyboard in Xcode11, delete the two settings in Info.plist, and then setapplication (_: didFinishLaunchingWithOptions:)of AppDelegate It is necessary to execute the processing that was added inscene (_: willConnectTo: options:)of SceneDelegate.

  • Answer # 2

    I haven't seen any reference videos, but I think it's an error around the storyboard, so if you don't want to use the storyboard, delete everything related to the storyboard Please give me.

    Delete Main.stroyboard file

    I think there was a setting to use storyboard Main somewhere in info.plist, so delete that line