因此,我试图在我的应用程序中添加一些功能,如果我按下+
按钮,我将被发送到页面。该页面将用于设置要放置在列表中的新项目。
但是,每当我按下+
按钮时,都会出现this class is not key value coding-compliant for the key dueDateField.
错误。我已经研究过,似乎与某些已删除的按钮有关。尽管错误文字说明它与dueDateField
有关。
到目前为止,我已经尝试恢复已删除的按钮,并且仍然出现相同的错误。该按钮与dueDateField
不相关。每次删除按钮时,我也会使用该按钮删除该操作。我还尝试取消对dueDateField
的注释,以查看它是否会产生一些不同的错误,但是没有。
ToDoViewController.swift
import UIKit
class ToDoViewController: UIViewController, UITextFieldDelegate {
// MARK: Properties
@IBOutlet weak var toDoNameField: UITextField!
@IBOutlet weak var workOnField: UITextField!
@IBOutlet weak var startTimeField: UITextField!
@IBOutlet weak var dueDateField: UITextField!
@IBOutlet weak var estTimeField: UITextField!
@IBOutlet weak var toDoNameLabel: UILabel!
@IBOutlet weak var workOnLabel: UILabel!
@IBOutlet weak var startTimeLabel: UILabel!
@IBOutlet weak var dueDateLabel: UILabel!
@IBOutlet weak var estTimeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Handles the changes for the labels through user input
toDoNameField.delegate = self
workOnField.delegate = self
startTimeField.delegate = self
dueDateField.delegate = self
estTimeField.delegate = self
}
// MARK: UITextFieldDelegate
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// Hide the keyboard.
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
// Determines which of the textfield is it working
switch(textField) {
case workOnField:
workOnLabel.text = "Work On: " + textField.text!
case startTimeField:
startTimeLabel.text = "Start Time: " + textField.text!
case dueDateField:
dueDateLabel.text = "Due Date: " + textField.text!
case estTimeField:
estTimeLabel.text = "Est. Time: " + textField.text!
default:
toDoNameLabel.text = textField.text
}
}
// MARK: Actions
/*@IBAction func setDefaultTodoName(_ sender: UIButton) {
toDoNameLabel.text = "Default ToDo Name"
}*/
}
ToDo.swift
import UIKit
class ToDo {
// MARK: Properties
var toDoName, workOnDate, startTime, dueDate, estTime: String
// MARK: Initializers
init?(toDoName: String, workOnDate: String, startTime: String,
dueDate: String, estTime: String) {
// Initialization should fail if either one of these items are set
if toDoName.isEmpty || workOnDate.isEmpty || startTime.isEmpty
|| dueDate.isEmpty || estTime.isEmpty {
return nil
}
}
}
ToDoTableViewCell.swift
import UIKit
class ToDoTableViewCell: UITableViewCell {
// MARK: Properties
@IBOutlet weak var startTimeLabel: UILabel!
@IBOutlet weak var toDoNameLabel: UILabel!
@IBOutlet weak var dueDateLabel: UILabel!
@IBOutlet weak var estTimeLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
ToDoTableViewController.swift
import UIKit
class ToDoTableViewController: UITableViewController {
// MARK: Properties
var toDos = [ToDo]()
override func viewDidLoad() {
super.viewDidLoad()
// Load the sample toDo data
loadSampleToDos()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Returns how many rows will be there for the list of Todos
return toDos.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Table view cells are reused when the user and should be dequeued using a cell identifer.
let cellIdentifier = "ToDoTableViewCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? ToDoTableViewCell else {
fatalError("The dequeued cell is not an instance of ToDoTableViewCell.")
}
// Retrieves the correct toDo for the layout of the data sourcer
let toDo = toDos[indexPath.row]
cell.toDoNameLabel.text = toDo.toDoName
cell.startTimeLabel.text = toDo.startTime
cell.dueDateLabel.text = toDo.dueDate
cell.estTimeLabel.text = toDo.estTime
return cell
}
// MARK: Private Methods
private func loadSampleToDos() {
// Sample todo items
guard let toDo1 = ToDo(toDoName: "Check my calendar", workOnDate: "5/24", startTime: "4:00 PM", dueDate: "6/07", estTime: "3 Hours") else {
fatalError("Unable to instantiate toDo1")
}
guard let toDo2 = ToDo(toDoName: "Watch a Game", workOnDate: "5/27", startTime: "5:00 PM", dueDate: "6/10", estTime: "1 Hour") else {
fatalError("Unable to instantiate toDo2")
}
guard let toDo3 = ToDo(toDoName: "Write a Story", workOnDate: "5/30", startTime: "4:30 PM", dueDate: "6/07", estTime: "1.5 Hour") else {
fatalError("Unable to instantiate toDo3")
}
toDos += [toDo1, toDo2, toDo3]
}
}
这是错误消息:
2019-06-13 23:17:14.754529-0500 ToDoNix [4333:171304] Interface Builder文件中的未知类_TtC7ToDoNix14ViewController。 2019-06-13 23:17:14.786783-0500 ToDoNix [4333:171304] *由于未捕获的异常'NSUnknownKeyException'而终止应用程序, 原因:'[ setValue:forUndefinedKey:]:此类不是键值 符合密钥DueDateField的编码要求。” * 第一个调用堆栈: ( 0 CoreFoundation 0x0000000107f601bb exceptionPreprocess + 331 1 libobjc.A.dylib 0x00000001065a3735 objc_exception_throw + 48 2 CoreFoundation 0x0000000107f5fd29-[NSException提高] + 9 3基础0x0000000105fcede4-[NSObject(NSKeyValueCoding)setValue:forKey:] + 292 4 UIKitCore 0x000000010a363292-[UIViewController setValue:forKey:] + 87 5 UIKitCore 0x000000010a5fa573-[UIRuntimeOutletConnection连接] + 109 6 CoreFoundation 0x0000000107f4bcfd-[NSArray makeObjectsPerformSelector:] + 317 7 UIKitCore 0x000000010a5f72b9-[UINib InstantiateWithOwner:options:] + 1814 8 UIKitCore 0x000000010a36a452-[UIViewController _loadViewFromNibNamed:bundle:] + 383 9 UIKitCore 0x000000010a36addc-[UIViewController loadView] + 177 10 UIKitCore 0x000000010a36b0ee-[UIViewController loadViewIfRequired] + 175 11 UIKitCore 0x000000010a36b940-[UIViewController视图] + 27 12 UIKitCore 0x000000010a2b5a9b-[UINavigationController _startCustomTransition:] + 931 13 UIKitCore 0x000000010a2cc3f0-[UINavigationController _startDeferredTransitionIfNeeded:] + 741 14 UIKitCore 0x000000010a2cd7e0-[UINavigationController __viewWillLayoutSubviews] + 150 15 UIKitCore 0x000000010a2ad600-[UILayoutContainerView layoutSubviews] + 217 16 UIKitCore 0x000000010ae74795-[UIView(CALayerDelegate)layoutSublayersOfLayer:] + 1441 17 QuartzCore 0x000000010c3fcb19-[CALayer layoutSublayers] + 175 18 QuartzCore 0x000000010c4019d3 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 395 19 QuartzCore 0x000000010c37a7ca _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 342 20 QuartzCore 0x000000010c3b197e _ZN2CA11Transaction6commitEv + 576 21 UIKitCore 0x000000010a984701 _UIApplicationFlushRunLoopCATransactionIfTooLate + 165 22 UIKitCore 0x000000010aa7e569 __handleEventQueueInternal + 6874 23 CoreFoundation 0x0000000107ec5721 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 24 CoreFoundation 0x0000000107ec4f93 __CFRunLoopDoSources0 + 243 25 CoreFoundation 0x0000000107ebf63f __CFRunLoopRun + 1263 26 CoreFoundation 0x0000000107ebee11 CFRunLoopRunSpecific + 625 27图形服务0x000000011011b1dd GSEventRunModal + 62 28 UIKitCore 0x000000010a98a81d UIApplicationMain + 140 29 ToDoNix 0x0000000105c6c917主+ 71 30 libdyld.dylib 0x00000001094a7575开始+ 1 ) libc ++ abi.dylib:以类型为NSException的未捕获异常终止 (lldb)