Getting NSManagedObject from NSSet

I have two objects with a one-to-many relationship between them. An object that has a lot has the expected NSSet property. I am not sure how to access a specific item in NSSet. NSSet contains objects that have several properties, one of which is currentWeek. I want to access an object in my NSSet that has a specific Week current.

I know I can do FetchRequest to find it, but I assume there is an easier way to use NSSet.

+4
source share
1 answer

You have several options.

NSArray* objectsArray = [yourSet allObjects]; 

This will populate the objectArray with all the objects in the set, after which you can list them by looking for the object or objects you want.

You can also use a predicate something like this:

 NSPredicate *desiredWeekPredicate = [NSPredicate predicateWithFormat:@"currentWeek == %d", currentWeekYouWant]; NSSet *objectsWithDesiredWeek = [yourSet filteredSetUsingPredicate:predicate]; 

(Your predicate will look different depending on how you store currentWeek). If you have only one object in currentWeek, you can simply call -anyObject onWithDesiredWeek objects to get your object. If you can have multiple objects with the same currentWeek, then calling the -allObjects method on objectsWithDesiredWeek will give you an array with all the objects that use the desired week.

+10
source

Source: https://habr.com/ru/post/1339840/


All Articles