gpt4 book ai didi

ios - Realm 结果 计数返回 nil

转载 作者:行者123 更新时间:2023-11-30 13:49:03 26 4
gpt4 key购买 nike

我正在使用 Realm “患者”对象填充 TableView 。为了获取我的表格 View 应该有多少行/部分,我运行:

class PatientsTableViewController: RealmSearchViewController {

var dataSource : Results<Patient>!
var currentPatient : Patient!
...

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

return dataSource.count
}

我有这个函数来设置dataSource,我在viewDidLoad中调用它

func reloadTable()
{
do
{
let realm = try Realm()
dataSource = realm.objects(Patient)
tableView?.reloadData()
}
catch
{

}
}

我在另一个函数中使用了currentPatient。但是,当我运行该应用程序时,它崩溃并给出错误

fatal error: unexpectedly found nil while unwrapping an Optional value

并指向

return dataSource.count

我尝试将其替换为 return dataSource!.count ,或在 viewDidLoad 中创建一个对象(仅用于测试,我不希望有任何对象我创建)但错误仍然存​​在,我也尝试重置模拟器中的内容和设置,但没有效果。我该怎么办?

最佳答案

我通过在初始 View Controller 中定义和初始化 mainDataSource 来修复此问题,然后通过 prepareForSegue 函数将其传递到下一个 View Controller 的 dataSource 应用程序曾经崩溃的位置。

关于ios - Realm 结果<T> 计数返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34557753/

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