[英]Realm Results<T> count returns nil
I'm populating my table view with Realm 'Patient' objects. 我正在用Realm“患者”对象填充表格视图。 To get how many rows/sections my tableview should have, i run :
要获取我的tableview应该有多少行/部分,我运行:
class PatientsTableViewController: RealmSearchViewController {
var dataSource : Results<Patient>!
var currentPatient : Patient!
...
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return dataSource.count
}
I have this function to set dataSource
and i call it in viewDidLoad
我有此功能来设置
dataSource
,我在viewDidLoad
调用它
func reloadTable()
{
do
{
let realm = try Realm()
dataSource = realm.objects(Patient)
tableView?.reloadData()
}
catch
{
}
}
I use the currentPatient
in another function. 我在另一个函数中使用
currentPatient
。 However, when i run the app, it crashes and gives me an error of 但是,当我运行该应用程序时,它崩溃并给我一个错误
fatal error: unexpectedly found nil while unwrapping an Optional value
致命错误:解开Optional值时意外发现nil
and points at 并指向
return dataSource.count
返回dataSource.count
I tried replacing it with return dataSource!.count
, or creating an object in the viewDidLoad
(just for testing, i don't want to have any objects that i create) but the error was still there, and i also tried to reset content and settings in the Simulator, but to no good. 我尝试用
return dataSource!.count
替换它,或者在viewDidLoad
创建一个对象(仅用于测试,我不想创建任何对象),但错误仍然存在,并且我还尝试重置内容和模拟器中的设置,但效果不佳。 What should i do ? 我该怎么办 ?
我通过在“初始视图控制器”中定义和初始化mainDataSource
并将其通过prepareForSegue
函数传递到应用程序曾经崩溃的下一个视图控制器的dataSource
来进行mainDataSource
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.