对实现比较的对象的 NSFetchRequest 结果进行排序:

Sorting the results of an NSFetchRequest of objects that implement compare:

我有一个实现 compare: 消息的 NSManagedObject 子类。我想检索这些对象并使用此比较方法对它们进行排序,但显然我必须为 NSSortDescriptor.

提供 key

我不想提供任何 key,因为对象本身已经知道如何比较自己。

如果我提供 nil 作为密钥的密钥:

r.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:nil
                                                        ascending:YES                                                        selector:@selector(compare:)]];

我收到以下错误:

failed: ((62) equal to ([[self.lib tags] count])) failed: throwing "keypath #self not found in entity "

如何确保 NSSortDescriptor 忽略任何 key 并只使用 compare:

您是否尝试提供密钥 @"self"

问题是我使用的是 SQLite 存储,您只能在依赖于 SQLite 存储的 NSFetchRequest 中使用建模属性。

这非常令人困惑,因为它在我的测试中有效(我使用的是内存存储)并且在应用程序本身中惨败。更好的错误描述会节省很多时间。