Realm 对象缺少除 primaryKey 之外的所有属性

Realm object is missing all properties except primaryKey

我有一个我的 tableViewDataSource 所依赖的 RealmObject 模型。 cellForRowAtIndexPath1 方法能够很好地访问属性,但是 didSelectRowAtIndexPath 方法从 Realm 对象获取所有空属性。我猜这与传递持久的 Realm 对象有关,但我不确定在哪里修复它。

//viewController...
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    let realm = try! Realm()
    let books = realm.objects(Book)
    viewModel = BookListViewModel(books: Array(books), onSelection: onSelection, onDeleteFailure: onDeleteFailure)
}
//end viewController

extension BookListViewModel: UITableViewDataSource {
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(BookTableViewCell.nibName, forIndexPath: indexPath) as! BookTableViewCell
        cell.bind(bookCellViewModels[indexPath.row])

        // NOTE:
        // bookCellViewModels[indexPath.row].book all properties are valid strings
        return cell
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return bookCellViewModels.count
    }
}

extension BookListViewModel: UITableViewDelegate {
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let book = bookCellViewModels[indexPath.row].book

        // NOTE:
        // all book properties are empty string or nil
        onSelection(book)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
}

没关系,原来是我的视图逻辑相关的bug。我只是感到困惑,因为 Xcode 调试器显示领域对象的所有空 属性 值,可能是因为它们都是动态属性。

您的调试器可能会向您显示 Realm 对象的所有空 属性 值,因为您缺少 LLDB plugin.

安装 Realm Xcode 插件

通过恶魔岛

LLDB 插件与 Realm Xcode plugin, which is available on Alcatraz.

捆绑在一起
  1. 安装 Alcatraz(如果尚未安装)。

    curl -fsSL https://raw.githubusercontent.com/supermarin/Alcatraz/deploy/Scripts/install.sh | sh
    
  2. 在 Xcode 中打开包管理器(⇧⌘9 或 Windows > 包管理器)

  3. 搜索 RealmPlugin

  1. 退出并重新启动 Xcode 以确保插件已加载

自己编译

您也可以通过打开 release zip 中包含的 plugin/RealmPlugin.xcodeproj 并单击构建来手动安装插件。

只安装 LLDB 插件

或者,您可以按照链接脚本的第一个代码注释中的建议安装它,或者只执行下面的脚本:

mkdir -p ~/Library/Application\ Support/Realm
wget -O ~/Library/Application\ Support/Realm/rlm_lldb.py https://raw.githubusercontent.com/realm/realm-cocoa/master/plugin/rlm_lldb.py
touch ~/.lldbinit
grep -q "rlm_lldb.py" ~/.lldbinit || echo "command script import "~/Library/Application Support/Realm/rlm_lldb.py" --allow-reload" >> .lldbinit