我做了一个结构Question
struct Question {
let imageView: UIImage
let textField: String
let textField2: String }类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")
] }如您所见,我创建了这个数组,并将UIImages与textField和textField2放在其中。简单地说,我将向用户显示一个图像,我将接受一个描述该图像的输入,并检查它是否与textField和textField2匹配。在运行模拟器时,我得到以下错误:
*由于未指明的异常'NSUnknownKeyException‘而终止应用程序,原因: setValue:forUndefinedKey::该类不符合键imageView的键值编码。以NSException类型的未明确例外终止
是因为我在数组中使用UIImage吗?
发布于 2021-04-26 11:13:23
根据您正在获得的错误消息:
终止应用程序,原因是:“ setValue:forUndefinedKey::该类不符合键imageView的键值编码。”以NSException类型的未明确例外终止“
几乎可以肯定的是,您的接口中有一个UIView类,它的属性imageView是无效的。我建议对字符串imageView执行多文件搜索。将搜索设置为整个单词,区分大小写。它会在故事板和源文件中找到字符串,所以你应该能够找到它。
发布于 2021-04-25 13:08:09
您的Question2类不是UIViewController或UIView的子类,因此在其中创建@IBOutlet是没有意义的。
当从故事板或nib文件创建视图控制器或视图时,将设置出口。你不会那么做的,所以这些网点不可能有价值。
为了您的目的,创建多个视图实例并将它们放入数组是没有意义的。
您应该使用Question结构,然后将该结构的实例提供给可以显示该结构的视图控制器。
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")
] }视图控制器将拥有这些出口,并且您将根据提供给它的Question分配这些插座的内容。
关于未定义键的异常显示,您的故事板或nib指定了一个UIView类(它没有任何出口属性),而不是使用这些IBOutlet属性创建的任何子类。
https://stackoverflow.com/questions/67253678
复制相似问题