How to fetch distinct values in Core Data?

Posted by Andy on Stack Overflow See other posts from Stack Overflow or by Andy
Published on 2010-05-07T02:39:24Z Indexed on 2010/05/07 2:48 UTC
Read the original article Hit count: 528

Filed under:
|

So in looking through Core Data Snippets, I found the following code:

...
[request setEntity:entity]; 
[request setResultType:NSDictionaryResultType];
[request setReturnsDistinctValues:YES];
[request setPropertiesToFetch:[NSArray arrayWithObject:@"<#Attribute name#>"]];

// Execute the fetch
NSError *error;
id requestedValue = nil; // WTF? This isn't defined or used anywhere
NSArray *objects = [managedObjectContext executeFetchRequest:request error:&error];
if (objects == nil) {
     // handle the error
}

This is great and seems perfect for what I need...but how does one actually use it? I assume since it's returning dictionaries, I need a key to get at the values - but where's the key defined? Is that the "id requestedValue = nil" line? If so, how does "requestedValue" become the key? Xcode gives me a compiler warning about an unused variable at the "requestedValue" declaration. I feel like I'm missing something here.

Thanks in advance for any assistance you can offer.

© Stack Overflow or respective owner

Related posts about iphone-sdk

Related posts about core-data