简体   繁体   中英

Put Core Data values in an array

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?

[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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM