我有一个 Realm 数据库,我在其中存储我的条目。存储和查询工作没有任何问题。我知道 Swift Realm 结果很懒惰,但我不明白为什么会发生这种情况。 我的代码:
newsItems = databaseHelper.retriveAllNews()
//[...] this is on tableView method
let currentNew = newsItems[indexPath.row]
print(currentNew)
newsCell.newsTitle.text = currentNew.title
newsCell.newsText.text = currentNew.info
print(..)
语句完美运行,我将所有信息存储在 currentNew
对象中,但是当我将信息设置到 UITextField 中时newsCell.newsTitle
和 newsCell.newsText
它们显示为空,因为结果尚未加载。
有什么建议吗?
最佳答案
您所看到的问题最可能的原因是在模型类的属性声明中省略了 dynamic
修饰符。 dynamic
修饰符是必要的,以确保 Realm 有机会拦截对属性的访问,让 Realm 有机会从磁盘上的文件读取/写入数据。省略这些属性会导致 Swift 编译器直接访问实例变量,从而将 Realm 排除在循环之外。
关于ios - 存储在 UITextField 上的 Realm 数据为空,print(entry) 有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38425721/