比较两个NSManagedObjects [英] Comparing two NSManagedObjects

查看:165
本文介绍了比较两个NSManagedObjects的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码循环通过一个NSManagedObjects数组,当它找到一个存储在实例变量中的某条记录时停止。我可以设法看看它们是否是相同的记录(不是等效记录,特定记录)的唯一方法是通过比较objectID的URIRepresentations。这肯定不是最好的做法。我在做:

I have some code that loops through an array of NSManagedObjects and stops when it finds a certain record that is stored in an instance variable. The only way I can manage to see if they are the same record (not an equivalent record, the specific record) is by comparing the URIRepresentations of the objectIDs. This surely cannot be the best way of doing it. I'm doing:

if ([[[obj1 objectID] URIRepresentation] isEqualTo: [[_obj2 objectID] URIRepresentation]]) {
  NSLog(@"Match");
}

下面的代码从不匹配,即使我NSLog的objectIDs,事实上完全一样。

The following code never matches even when I NSLog the objectIDs and see that they are in fact exactly the same.

if ([[obj1 objectID] isEqualTo: [_obj2 objectID]]) {
  NSLog(@"Match");
}


推荐答案

注释器正确,isEqualTo :在这种情况下不会工作,因为它们是NSManagedObjectID的不同实例。

The commenter is correct, isEqualTo: will not work in this case since they are different instances of NSManagedObjectID.

你这样做的方式实际上是最好的方法,objectID是CoreData的唯一标识符给定管理对象,它是唯一的方法来告诉NSManagedObject的两个实例是否指向持久存储中的同一记录。

The way you are doing this is actually the best way, the objectID is CoreData's unique identifier for a given managed object, it's the only way to tell if two instances of NSManagedObject point to the same record in the persistent store.

这篇关于比较两个NSManagedObjects的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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