gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:51:38 26 4
gpt4 key购买 nike

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

我已经尝试解决这个问题,但在应用程序的另一部分的主从屏幕上没有这个问题,所以我觉得这个问题的发生要么是因为两个 TableView ,要么是因为 Realm 数据库的某些问题.

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

Unexpected Error

客户端.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>()

}

文档.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]

目标 View Controller 中的 socket 尚未连接,因此 titleLabeldidSet 中为 nil


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

var selectedDocument : Document?

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

关于swift - 由于 nil 可选值,无法在详细信息 View 中设置 Realm 数据库对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54505667/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com