我对此都有自己的文档列表的客户对象境界数据库。在我SplitViewController我有一个显示所有已添加的客户端实现代码如下。当点击一个客户端,我可以显示在另一个实现代码如下特定客户端的所有文件。出现的问题是点击一个文件时,应用程序崩溃和详细视图控制器提供了以下错误:线程1:致命错误:意外地发现零而展开的可选值。
我试图解决此问题,所以我觉得这个问题是因为两个tableviews或因为一些问题与境界数据库发生没有与主从屏幕这个问题在应用程序的其他部分。
一个奇怪的是我得到同样的致命错误,甚至当我指定标签自己的价值,而无需使用来自我的数据库值。
Client.swift
import Foundation
import RealmSwift
class Client: Object {
@objc dynamic var name: String = ""
@objc dynamic var age: Int = 0
@objc dynamic var bond: Double = 0.0
@objc dynamic var continuances: Int = 0
//Realm syntax to define a to-many relationship meaning each Client can have a number of Document objects
let documents = List<Document>()
}
Document.swift
import Foundation
import RealmSwift
class Document: Object {
@objc dynamic var title: String = ""
@objc dynamic var image: String = ""
//An inverse relationship with each Document having an associatedClient that is of the
//type Client and it comes from the property called "documents" from the Client forward relationship
var associatedClient = LinkingObjects(fromType: Client.self, property: "documents")
}
DocumentTableViewController.swift
import UIKit
import RealmSwift
class DocumentTableViewController: UITableViewController {
let realm = try! Realm()
var documents : Results<Document>?
//Data type optional document because it will be nil until it is set
var selectedClient : Client? {
//Everything in the did set function will happen as soon as this variable is set with a value
didSet {
loadDocuments()
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
//Mark - Model Manipulation Methods
func loadDocuments(){
//Pulls all document objects out of our realm (persistent data)
documents = selectedClient?.documents.sorted(byKeyPath: "title", ascending: true)
tableView.reloadData()
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return documents?.count ?? 0
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "goToDocumentDetails", sender: self)
}
//Load the documents for the selected client
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let navigationController = segue.destination as? UINavigationController
let viewController = navigationController?.topViewController as? DocumentDetailsViewController
if let indexPath = tableView.indexPathForSelectedRow {
viewController?.selectedDocument = documents?[indexPath.row]
}
}
//MARK: - HELPER FUNCTIONS FOR TESTING
@IBAction func addTestDocument(_ sender: UIBarButtonItem) {
if let currentClient = self.selectedClient {
do {
try self.realm.write {
let newDocument = Document()
newDocument.title = "TEST"
currentClient.documents.append(newDocument)
print(documents?.count ?? 0)
}
} catch {
print("Error saving new document")
}
}
loadDocuments()
}
}
DocumentDetailsViewController.swift
import UIKit
import RealmSwift
class DocumentDetailsViewController: UIViewController {
let realm = try! Realm()
//var documents : Results<Document>?
@IBOutlet weak var titleLabel: UILabel!
//Data type optional document because it will be nil until it is set
var selectedDocument : Document? {
//Everything in the did set function will happen as soon as this variable is set with a value
didSet {
titleLabel.textColor = UIColor.blue
titleLabel.text = selectedDocument?.title
}
}
我希望我能够通过在选定文档对象,但试图以任何方式改变后的值编程,以改变我的DocumentsDetailViewController值产生零可选值异常。
这是一个常见的错误/误解。它不涉及到境界的。
在你打电话的那一刻
viewController?.selectedDocument = documents?[indexPath.row]
在目标视图控制器出口尚未连接,因此titleLabel
是nil
didSet
。
一种可能的解决方案是移动中的代码didSet
到viewDidLoad
var selectedDocument : Document?
override func viewDidLoad() {
super.viewDidLoad()
titleLabel.textColor = UIColor.blue
titleLabel.text = selectedDocument?.title
}