由于 nil 可选值,无法在详细信息视图中设置 Realm 数据库对象的属性

Unable to set properties of Realm database object in Detail View because of nil optional value

我有一个包含客户端对象的领域数据库,每个对象都有自己的文档列表。在我的 SplitViewController 中,我有一个显示所有添加的客户端的表格视图。单击客户时,我可以在另一个表格视图中显示该特定客户的所有文档。发生的问题是单击文档时应用程序崩溃并且详细信息视图控制器给出以下错误:线程 1:致命错误:在展开可选值时意外发现 nil。

我已经尝试解决这个问题,但在应用程序的另一部分的主从屏幕上没有遇到这个问题,所以我觉得这个问题的发生要么是因为两个表格视图,要么是因为领域数据库。

一件奇怪的事情是,即使我自己分配标签的值而不使用数据库中的值,我也会遇到同样的致命错误。

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
    }
}

我希望在传入选定的 Document 对象后能够以编程方式更改 DocumentsDetailViewController 中的值,但尝试以任何方式更改值都会产生 nil 可选值异常。

这是一个常见的错误/误解。跟Realm一点关系都没有。

你打电话的那一刻

viewController?.selectedDocument = documents?[indexPath.row]

目标视图控制器中的出口尚未连接,因此 titleLabeldidSet 中是 nil


一个可能的解决方案是将 didSet 中的代码移动到 viewDidLoad

var selectedDocument : Document?

override func viewDidLoad() {
    super.viewDidLoad()
    titleLabel.textColor = UIColor.blue
    titleLabel.text = selectedDocument?.title
}