问题描述
我有一个 NSMangedObject
,其中包含 NSSet
其他 NSManagedObjects
我需要检查这些对象是否在 NSSet
中有一个值,然后返回它们。 p>
我使用MagicalRecord来获取数据。
所以我需要这样的:
NSPredicate * predicate = [NSPredicate predicateWithFormat:@stringObjects contains%@,string];
所以如果NSSet stringObjects包含一些我正在寻找的字符串,那么返回对象我已经请求。 / p>
注意这里:stringObjects(名称只是为了代表我的 NSSet
)它是 NSSet
包含NSManagedObjects,所以我需要通过一些id(例如string_id属性)来搜索它们。
/ p>
NSSet * set = MainObject.stringObjects;
NSString * string_id_for_first_object = [[[set allObjects] objectAtIndex:0] string_id];
只是为了更好地理解关系。
但是问题是如何创建谓词来检查NSSet是否包含所需的id。
如果我正确理解你的问题, stringObjects
是从一个实体 A 到另一个实体 B 的一对多关系
, > B 具有 string_id
的属性。
与给定 string id
的任何 B 对象相关的对象,请使用:
NSString * stringId = ...; //你正在寻找的字符串
NSPredicate * predicate = [NSPredicate predicateWithFormat:@ANY stringObjects.string_id ==%@,stringId];
I have an NSMangedObject
that contains NSSet
of other NSManagedObjects
.
I need to check if these objects has an value in NSSet
and then return them.
I use MagicalRecord for fetching data.
So I need something like this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"stringObjects contains %@", string];
So if NSSet stringObjects contains some string that I am looking for so then return object I have requested.
One note here: stringObjects (name just for example it represent my NSSet
) it is NSSet
that contains NSManagedObjects, so I need to search them by some id (for example string_id attribute).
So then model looks like this
NSSet *set = MainObject.stringObjects;
NSString *string_id_for_first_object = [[[set allObjects] objectAtIndex:0] string_id];
Just for better understanding relationship.
But the question is about how can I create predicate to check if NSSet contains needed id.
If I understand your question correctly, stringObjects
is a to-many relationshipfrom one entity A to another entity B, and B has an attribute string_id
.
To find all A objects that are related to any B object with the given string id
, use:
NSString *stringId = …; // The string that you are looking for
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY stringObjects.string_id == %@", stringId];
这篇关于NSPredicate在NSSet中找到具有属性的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!