【问题标题】:Realm object is missing all properties except primaryKey领域对象缺少除 primaryKey 之外的所有属性
【发布时间】:2015-11-19 14:09:26
【问题描述】:

我有一个我的 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)
    }
}

【问题讨论】:

    标签: ios swift realm


    【解决方案1】:

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

    安装 Realm Xcode 插件

    通过恶魔岛

    LLDB 插件与Realm Xcode plugin 捆绑在一起,可在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 插件

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

    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
    

    【讨论】:

      【解决方案2】:

      没关系,事实证明这是与我的视图逻辑相关的错误。我只是感到困惑,因为 Xcode 调试器显示了领域对象的所有空属性值,可能是因为它们都是动态属性。

      【讨论】:

        猜你喜欢
        • 2018-10-26
        • 2022-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多