为什么无法获取已过滤领域列表的索引? [英] Why can't I get the index of a filtered realm List?

查看:92
本文介绍了为什么无法获取已过滤领域列表的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将项目追加到列表对象之后,我正在尝试在List <>对象中找到该项目的索引,以便可以将其插入到表格视图中.

I'm trying to find the index of an item in a List<> object after the item has been appended to it, so that I can insert into a tableview.

tableview被.filter所分割,因此我必须在查找indexPath之前应用过滤器.但是,该过滤器似乎破坏了indexOf功能.

The tableview is sectioned with .filters so I have to apply the filter before looking for the indexPath. However, the filter appears to break the indexOf functionality.

我注意到函数.map具有相同的作用.

I noticed that the function .map has the same effect.

import UIKit
import RealmSwift

class Model: Object {
    @objc dynamic var title: String = ""
    let items = List<Model>()
}

class ViewController: UIViewController {

    var models: Results<Model>?
    var parentModel: Model?
    var items = List<Model>()
    let realm = try! Realm()

    override func viewDidLoad() {
        super.viewDidLoad()

        if !UserDefaults.standard.bool(forKey: "IsNotFirstTime") {
            populateRealm()
            UserDefaults.standard.set(true, forKey: "IsNotFirstTime")
        }

        models = realm.objects(Model.self)

        parentModel = models!.first
        items = parentModel!.items

        let child = Model()
        child.title = "Child"

        try! realm.write {
            parentModel!.items.append(child)
        }

        print(items.index(of: child)) // prints correct value
        print(items.filter({ $0.title == "Child" }).index(of: child)) // prints nil
    }

    func populateRealm() {
        let parent = Model()
        parent.title = "Parent"
        try! realm.write {
            realm.add(parent)
        }
    }
}

尽管映射没有整体效果,但第一个打印件找到了对象,但是第二个打印件没有找到对象.

The first print finds the object, but the second print doesn't, despite the mapping having no overall effect.

奇怪的是,对象位于过滤列表中,执行以下操作:

The strange thing is that the object IS in the filtered list, doing:

print(items.filter({ $0.title == "Child" }).first

返回对象,因此它就在那里.

Returns the object, so it is there.

进一步检查,看起来不是过滤器,而是数组类型的转换破坏了功能,没有过滤器的情况下转换为数组的作用相同.

On further inspection, it looks like it's not the filter but the conversion of array type that breaks the functionality, converting to array without a filter does the same thing.

print(Array(items).index(of: child)) // prints nil

推荐答案

我想出了解决方案.我使用的.filter({ $0.title == "Child" })过滤器语法不是Realm过滤器,而是将List转换为LazyFilterCollection<List<Model>>,这似乎与搜索领域对象的索引不兼容.

I figured out the solution. The filter syntax I used .filter({ $0.title == "Child" }) isn't the Realm filter, and converts the List to a LazyFilterCollection<List<Model>>, which doesn't seem to be compatible with searching for the index of a realm object.

解决方法是使用.filter("title == %@", "Child")格式,该格式返回领域结果对象.

The fix was to use the format .filter("title == %@", "Child"), which returns a realm Results object.

这篇关于为什么无法获取已过滤领域列表的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆