Ahmed AlFailakawi
Ahmed AlFailakawi

Reputation: 29

Is It Possible to Use UIImage Inside an Array of Objects?

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?

Upvotes: 0

Views: 65

Answers (2)

Duncan C
Duncan C

Reputation: 131418

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.

Upvotes: 0

Paulw11
Paulw11

Reputation: 114836

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

Upvotes: 1

Related Questions