0

I need to put the values retrieved using a Fetch Request from my core data graph into an array, but not entirely sure how to go about this.

I'm using the following to perform the fetch:

    NSString *entityName = @"Project"; // Put your entity name here
        NSLog(@"Setting up a Fetched Results Controller for the Entity named %@", entityName);

        // 2 - Request that Entity
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];

        // 3 - Filter it if you want
        request.predicate = [NSPredicate predicateWithFormat:@"belongsToProject = %@", _selectedProject];

        // 4 - Sort it if you want
        request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"dateTaken"
                                                                                         ascending:YES
                                                                                          selector:@selector(localizedCaseInsensitiveCompare:)]];
        // 5 - Fetch it
        self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                            managedObjectContext:self.managedObjectContext
                                                                              sectionNameKeyPath:nil
                                                                                       cacheName:nil];


[self performFetch];

As you can see, i'm filtering the values that come back using an NSPredicate.

How would I get these values into an array and also be able to select individual attributes for the entity once they are in the array, for example the project.description or project.name?

Thanks to Eimantas I've got the objects in an array, however I still need to do two things:

  1. Loop through the array and output the data into some HTML
  2. Individually select attributes from the array, for example, the project description.

I'm using the following for loop to do the first:

for (int i=0; i < [projectListArray count]; i++) 
{
        NSString *tmp = (NSString *)[projectListArray objectAtIndex:i];
}

However, this is returning the error:

-[Project length]: unrecognized selector sent to instance 0x1b9f20
2012-03-28 10:48:35.160 Project App[3973:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Project length]: unrecognized selector sent to instance 0x1b9f20'

It appears as though i might not be incrementing?

1 Answer 1

1

[self.fetchedResultsController fetchedObjects] returns the array of fetched objects.

update

It's better to use fast enumaration, not a for loop:

for (Project *project in projectListArray) {
    NSString *projectDescription = [project valueForKey:@"description"];
}

You're getting the exception because you're casting an object to NSString while it's a pointer to (I presume) Project managed object.

Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant, thankyou Eimantas. I have one alst thing I'm trying to do, if you wouldn't mind helping? I've updated my original question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.