requestedValue is nothing and is obviously not being used (perhaps it was part of some other example that got cut&paste into this example code--who knows). You can just ignore it.
The keys for the dictionary are the same as the attributes you fetched and are defined in your model. So, for example, if you had a person entity with three attributes defined, name, age, phoneNumber, and you requested only name, that would be the only keys with data in your dictionaries. So:
[request setEntity:entity];
[request setResultType:NSDictionaryResultType];
[request setReturnsDistinctResults:YES];
[request setPropertiesToFetch:[NSArray arrayWithObject:@"name"]];
// Execute the fetch
NSError *error;
NSArray *objects = [managedObjectContext executeFetchRequest:request error:&error];
if (objects == nil) {
abort();
}
for( NSDictionary* obj in objects ) {
NSLog(@"Person: %@", [obj objectForKey:@"name"]);
}
// ...
So, if you have 5 people in your data store named Bob, Sally, Joe, Freida and Sue, you would see those names print out. If you want to use any of the other attributes (like their age), you would have to add that to the array you set in setPropertiesToFetch:
.
In most cases, it is probably best to simply retrieve the managed object, however. The object will be faulted so you won't even bring attributes into memory unless you actually access them. Plus, if you want to make changes to the objects, you could and persist them to the store (with managed objects).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…