MacUser
MacUser

Reputation: 491

Core data number of attributes in Entity

I am new to core data I would appreciate some assistance in the code below that I need to display the number of attributes in an entity;

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];

    //NSUInteger attributeCount = [[[object entity] attributesByName] count];
    float valueSF = 0;
    for (NSManagedObject *object in [sectionInfo objects]) {
        NSUInteger attributeCount = [[[object entity] attributesByName] count];
        valueSF += [[object valueForKey:@"value"] floatValue];
        valueSF = valueSF / attributeCount;
    }
    return [NSString stringWithFormat:@"[Average = %.03f] [Cases = %i]", valueSF, [[sectionInfo objects] count]];

}

Upvotes: 0

Views: 690

Answers (2)

S. Karthik
S. Karthik

Reputation: 618

It's easy now. Number of Attributes for the Particular entity capture by below function:

NSInteger count=[managedObjectContext countForFetchRequest:fetchRequest error:&error];

Upvotes: 0

Tom Harrington
Tom Harrington

Reputation: 70956

Core Data provides good introspection. Assuming you mean the number of attributes actually declared for the entity in the data model, you'd want:

NSUInteger attributeCount = [[[object entity] attributesByName] count];

If you want to include the number of relationships, replace attributesByName with propertiesByName.

Upvotes: 4

Related Questions