0

I have used core data in my ios app. Now i have one table in which there are two columns .

  1. Category

  2. Order (In which there are NSNumber 1, 2 ,5)

I want to fetch data so It will first sort alphabetically by Category name and than by Order Number.

I have used below code :

NSEntityDescription *entity_Maintness = [NSEntityDescription
                                             entityForName:@"Maintness" inManagedObjectContext:__managedObjectContext];
    [fetchRequest_Maintness setEntity:entity_Maintness];


    NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"type" ascending:YES];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"serialNumber" ascending:NO];


    NSArray *sortDescriptors12 = [[NSArray alloc] initWithObjects:sortDescriptor1, sortDescriptor, nil];
    [fetchRequest_Maintness setSortDescriptors:sortDescriptors12];

But the data are sorted only by Category not by Serial Number.

Thanks for Help.

4

1 回答 1

1

上面的代码看起来没问题。也许您的代码中的其他地方存在问题?这是我为您快速生成的一些代码,可以作为起点??

#import "MainViewController.h"
#import "Maintness.h"

@interface MainViewController ()
@property (weak, nonatomic) IBOutlet UITextField *type;
@property (weak, nonatomic) IBOutlet UITextField *serialNumber;
@property (strong, nonatomic) NSManagedObjectContext *context;
- (IBAction)addButtonPressed:(id)sender;
- (IBAction)retrieveButtonPressed:(id)sender;

@end

@implementation MainViewController

#pragma mark - lazy instantiation
- (NSManagedObjectContext *)context{
    if (!_context){
        id appDelegate = (id)[[UIApplication sharedApplication] delegate];
        _context = [appDelegate managedObjectContext];
    }
    return _context;
}

#pragma mark - core data interactions
- (IBAction)addButtonPressed:(id)sender {
    NSError *error = nil;

    Maintness *newMaintness = nil;

    newMaintness = [NSEntityDescription insertNewObjectForEntityForName:@"Maintness" inManagedObjectContext:self.context];

    newMaintness.type = self.type.text;
    newMaintness.serialNumber = self.serialNumber.text;

    if (![self.context save:&error]) {
        NSLog(@"Oh no - error: %@", [error localizedDescription]);
    } else {
        NSLog(@"It appears the details were added ok");
    }

}

- (IBAction)retrieveButtonPressed:(id)sender {
    NSError *error = nil;
    // Set up fetch
    NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
    // Set up entity
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Maintness" inManagedObjectContext:self.context];
    [fetch setEntity:entity];
    // Set up sorting
    //   - sorts in order of array
    NSSortDescriptor *primarySortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"type" ascending:YES];
    NSSortDescriptor *secondarySortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"serialNumber" ascending:NO];
    NSArray *sortDescriptors = @[primarySortDescriptor, secondarySortDescriptor];
    [fetch setSortDescriptors:sortDescriptors];

    NSArray *results = [self.context executeFetchRequest:fetch error:&error];

    for (Maintness *result in results) {
        NSLog(@"type: %@ serial number: %@", result.type, result.serialNumber);
    }
}
@end

在此处输入图像描述

于 2013-09-15T05:24:43.653 回答