I've got an entity
called clients and an NSArrayController
called clientsController. In my clients entity I have an attribute called clientCompany.
What is the most straightforward way of programmatically getting a list of each clientCompany while running a loop (so that code can follow on from each discovered clientCompany)? I'm not sure whether I should be accessing the array controller or the managed object in this case.
I've tried:
for (NSDictionary *key in clientsController) {
NSLog(@"%@", [key objectForKey:@"clientCompany"]);
}
with no luck, but I think I'm way off on that. I've also tried printing the arrangedobjects of the array, out of curiosity, but it prints empty:
NSLog(@"%@", [clientsController arrangedObjects]);
clientsController has been initialised and accessed elsewhere in the program so I thought it would be straightforward but I'm very new to all this. Thanks.
Update
I've had a sma开发者_如何学Goll bit of success going the
NSManagedObjectContext
route and trying NSFetchRequest
. Requesting ObjectAtIndex:0
I can now print the first clientCompany string to the console. I just need to be able to pull all them out in a loop but the hard part is done (I hope).This worked for me:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *clientsMoc= [clientsController managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Clients" inManagedObjectContext:clientsMoc];
[fetchRequest setEntity:entity];
NSError *error = nil;
NSArray *items = [clientsMoc executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];
NSInteger *counter;
counter = 0;
for (NSString *s in items) {
NSManagedObject *mo = [items objectAtIndex:counter]; // assuming that array is not empty
id value = [mo valueForKey:@"clientCompany"];
NSLog(@"a value is %@", value);
counter = counter + 1;
}
精彩评论