class is not key value coding-compliant for the key mainScrollView

I am working on a new project, String Converter which will be run on a device supporting iOS 13.0 and up. My problem is in the Tab Layout, specifically the encoder tab I have a UIScrollView (and UIView inside that module) set up and linked as "mainScrollView" in the custom class "FirstViewController.swift".

The outlet for mainScrollView is in the class FirstViewController which is set as the custom class for the Encoder Scene. The mainScrollView (UIScrollView) outlet is set as linked to the UIScrollView module object in that specific scene (Encoder Scene). The link is displayed as set in the "Connection Inspector" under "Outlets" and as said previously that UIScrollView module is in the FirstViewController class.

Despite everything (seemingly) being there, I am still getting this error message (which is displayed when the app terminates and takes me to the AppDelegate.swift file):

Thread 1: Exception: "[<UIViewController 0x7f85ea557bb0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key mainScrollView.




Answered by Claude31 in 632981022

I did accidentally delete my Main.storyboard though...

Just this !?!
Of course, it puts havoc in the whole project, no surprise you get such crash.

You should have said earlier. You should now close the thread.

error message (which is displayed when the app terminates 

Do you mean when app terminates on crash or when you terminate it yourself ?

Is it a SwiftUI project ?

Your explanation is a bit hard to follow, so I try to reformulate. Tell if that's exactly the setup.
  • You have defined a FirstViewController class

  • Inside you have an IBOutlet,

@IBOutlet var mainScrollView : UIScrollView
  • You have defined a ViewController in Storyboard named Encoder Scene

  • You declared the VC as FirstViewController in Identity Inspector

So, 2 questions:
=> Is there an Encoder class ?
=> How is it that ViewController does not show as FirstViewController Scene

It would help in fact to see some code.



Thank you for the reply. I am currently busy with work but when I get off I will post a code and a screenshot of the main workspace layout. Yes, I meant the app terminates in crashes. Yes the setup is correct. I’m sorry if my question was hard to follow this is my first time posting an in a forum. I will definitely post code in a screenshot when I get the chance.

=> Is there an Encoder class ?

There is no encoder class just the first view control Class in that file.

=> How is it that ViewController does not show as FirstViewController Scene

I renamed the title If that ViewController.



Have you noticed this part in the error message?

<UIViewController 0x7f85ea557bb0>

One thing sure is that iOS created an instance of UIViewController (not FirstViewController) and tried to set the IBOutlet mainScrollView.
Of course, UIViewController does not have an outlet with such a name, so, crash.

This may happen when your storyboard is sort of broken and the Custom Class settings has gotten no effect.

Try removing the Encoder Scene and re-create and re-configure it.
I tried OOPer's solution but to no avail, I have screenshot the important bits which includes the code and the main view along with the Outlet connection for UIScrollView. Everything seems to be in tack...but I don't know what's wrong...I will start another iteration of this project to test out if the view is actually broken.

Edit: Cannot attach images so, I will attach the code for FirstViewController.swift

Code Block
import UIKit
class FirstViewController: UIViewController, UIScrollViewDelegate {
    // MARK: Outlet prototype initializations
    @IBOutlet weak var mainScrollView: UIScrollView!
  
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
// For scroll view scroll functionality
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        
        mainScrollView.contentSize = CGSize(width: mainScrollView.contentSize.width, height: mainScrollView.contentSize.height)
        mainScrollView.decelerationRate = UIScrollView.DecelerationRate.fast
    }
}


Update: I began a new project iteration, String Converter 2, and I did the same thing as I did last time and now the program works...so...I don't know what happened, but now the program works. I have kept the original (broken) program just in case anyone is able to solve the problem with the first (original) program.
In the broken project, did you do an option - clean build folder ?
May be you broke something when you renamed the ViewController in Interface Builder.

Note: I find it more convenient to have the same name for the class and the VC (here you could rename FirstViewController as Encoder). But that's just a personal choice.
I was messing with the constraints, trying to fix the constraints using auto-layout, other than that, I didn't do much of anything. I did accidentally delete my Main.storyboard though...I think that would have been the cause.
Accepted Answer

I did accidentally delete my Main.storyboard though...

Just this !?!
Of course, it puts havoc in the whole project, no surprise you get such crash.

You should have said earlier. You should now close the thread.

I think that would have been the cause.

In fact, I do not know all the cases which causes this sort of broken storyboard. So, what you have done may be one possibility.
But there are many others, using Pods, renaming class names are what I have experienced most often till now.

Anyway, you can find that the storyboard is broken by seeing the instance type contained in the error message.
When an unexpected type was found, you may need to
  • replace some parts of the storyboard content

or
  • replace whole storyboard

or else
  • start a new project

Thank you for the help and baring with me. I have completely forgotten about the Main.storyboard deletion 😂.

I got the error with myTextField. My app crashes and gives me the error when I click on a button. But myTextField has nothing to do with the vc the button is on and other parts of my app work but this and a few other buttons are driving me crazy. I tried to search for answers but nothing helped. Please help!

class is not key value coding-compliant for the key mainScrollView
 
 
Q