मैं इस विधि (मोटे तौर पर स्वीकार किए जाते हैं जवाब के समान) को खोजने के एक छोटे से क्लीनर और आसान हो करने के लिए समझने के लिए।
SELECT COUNT(*), a_value FROM my_table GROUP BY a_value;
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:[MyTable className]];
// create expression for grouped column
NSExpressionDescription *aValueDescription = [[NSExpressionDescription alloc] init];
aValueDescription.name = @"aValue"; // name of key in result dictionary
aValueDescription.expression = [NSExpression expressionWithFormat:@"aValue"];
aValueDescription.expressionResultType = NSObjectIDAttributeType;
// create expression for count
NSExpressionDescription *countDescription = [[NSExpressionDescription alloc] init];
countDescription.name = @"count"; // name of dictionary key in result dictionary
countDescription.expression = [NSExpression expressionWithFormat:@"[email protected]"];
countDescription.expressionResultType = NSInteger32AttributeType;
// fill out fetch request
fetchRequest.propertiesToGroupBy = @[@"aValue"];
fetchRequest.propertiesToFetch = @[aValueDescription, countDescription];
//fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"count" ascending:NO]]; // not sure why this crashes
fetchRequest.resultType = NSDictionaryResultType; // required for "group by" requests
NSError *error = nil;
NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
लौटे results
NSDictionary की एक सरणी है: यह करने के लिए एसक्यूएल बराबर है। ध्यान दें कि वर्णन name
गुण आपकी इच्छानुसार कुछ भी हो सकते हैं - वे केवल लौटे गए शब्दकोशों में कुंजी के नाम हैं। तालिका से पंक्तियों को फ़िल्टर करने के लिए fetch अनुरोध में कोई भविष्यवाणी कर सकता है; यह कोड सभी पंक्तियों को वापस करता है।
बोनस किसी को अंक जो मुझे बताओ कैसे प्रकार वर्णनकर्ता काम करने के लिए कर सकते हैं ...
करें यदि एक जवाब आप बाहर की मदद की तो हमें बताएं। –