0

I made a struct Question

struct Question {

let imageView: UIImage
let textField: String
let textField2: String }

class SpellingViewController

class SpellingViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var theImage: UIImageView!
@IBOutlet weak var theInput: UITextField!
@IBOutlet weak var nextButton: UIButton!
@IBOutlet weak var progBar: UIProgressView!

var spellingScore: Int64 = 0
var questionNum = 0

let question = [
    Question2(imageView: UIImage(named: "BeardQ")!, textField: "Beard", textField2: "beard"),
    Question2(imageView: UIImage(named: "CastleQ")!, textField: "Castle", textField2: "castle"),
    Question2(imageView: UIImage(named: "CloudQ")!, textField: "Cloud", textField2: "cloud"),
    Question2(imageView: UIImage(named: "Elephant")!, textField: "Elephant", textField2: "elephant"),
    Question2(imageView: UIImage(named: "RainQ")!, textField: "Rain", textField2: "rain")
] }

As you can see, I created this array and placed the UIImages inside it along with the textField and textField2. Simply, I'm gonna display an image to the user and I'm gonna take an input that describes the image and examine if it matches textField and textField2 or not. While running the simulator, I get the following error:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIView 0x7f9987017e80> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key imageView.' terminating with uncaught exception of type NSException

Is it because I'm using UIImage inside an array?

2 Answers 2

1

Your Question2 class is not a subclass of UIViewController Or UIView, so it doesn’t make sense to create @IBOutlet s in it.

Outlets are set when a view controller or view is created from a storyboard or nib file. You aren’t doing that, so there is no way those outlets will have a value.

It doesn't make sense to be creating multiple instances of views and putting them in an array anyway for your purpose.

You should use a Question struct and then supply an instance of that struct to a view controller that can display it.

struct Question {
   let image: UIImage
   let txt: String
   let txt2: String
}

let question = [
    Question(img: UIImage(named: "BeardQ")!, txt: "Beard", txt2: "beard"),
    Question(img: UIImage(named: "CastleQ")!, txt: "Castle", txt2: "castle"),
    Question(img: UIImage(named: "CloudQ")!, txt: "Cloud", txt2: "cloud"),
    Question(img: UIImage(named: "Elephant")!, txt: "Elephant", txt2: "elephant"),
    Question(img: UIImage(named: "RainQ")!, txt: "Rain", txt2: "rain")
] }

The view controller will have the outlets and you will assign to the content of those outlets based on the Question that is provided to it.

The exception regarding an undefined key shows that your storyboard or nib has specified a class of UIView (Which doesn’t have any outlet properties) rather than whatever subclass you have created with those IBOutlet properties

Sign up to request clarification or add additional context in comments.

6 Comments

No, the OPs Question class is just a class object, not a subclass of UIViewController. It has @IBOutlets, which does not make sense. Your solution of storing the image as a property of the object does make sense, and seems like the right solution. (Voted.)
I created the struct and supplied the instance to the VC, however, I'm still getting the same error, which is confusing a little bit.
@DuncanC, thanks. I saw the outlets and assumed it was a view controller.
@AhmedAlFailakawi edit your question to show your new code, including the line on which it is crashing. Usually that error message means you have an IBOutlet/IBAction in your storyboard that points to target that doesn't have a matching property.
@DuncanC The error is a run time error it's saying __pthread_kill with a given address. I made sure that the IBOutlets are correctly connected from the storyboard to the VC. Question has been updated
|
0

Based on the error message you are getting:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIView 0x7f9987017e80> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key imageView.' terminating with uncaught exception of type NSException"

It is almost certain that you have a UIView class in your interface somewhere with a property imageView which is not valid. I suggest doing a multi-file search for the string imageView. Set the search to whole words, case sensitive. It finds strings inside Storyboards as well as source files, so you should be able to find it that way.

2 Comments

Yup! You are absolutely right. When I checked the storyboard and deleted the outlet in my VC, I discovered that I, unfortunately, connected the wrong imageView to this VC. Thanks a lot for your tips and help!
If my answer solved your problem then you should accept it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.