Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSSortDescriptor问题_Ios_Objective C_Cocoa Touch_Core Data_Nssortdescriptor - Fatal编程技术网

Ios NSSortDescriptor问题

Ios NSSortDescriptor问题,ios,objective-c,cocoa-touch,core-data,nssortdescriptor,Ios,Objective C,Cocoa Touch,Core Data,Nssortdescriptor,我目前正在对一个名为“视频”的实体进行排序。我正在尝试对所有视频进行排序,首先是按录制日期排序,格式为“2013年12月21日”,然后是标题,这只是一个字符串。以下是我正在使用的代码: - (void)viewWillAppear:(BOOL)animated { self.title = [NSString stringWithFormat:@"Videos of %@", _currentAthlete.full]; AppDelegate *appDelegate =

我目前正在对一个名为“视频”的实体进行排序。我正在尝试对所有视频进行排序,首先是按录制日期排序,格式为“2013年12月21日”,然后是标题,这只是一个字符串。以下是我正在使用的代码:

- (void)viewWillAppear:(BOOL)animated {

    self.title = [NSString stringWithFormat:@"Videos of %@", _currentAthlete.full];

    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    _managedObjectContext = [appDelegate managedObjectContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *video = [NSEntityDescription entityForName:@"Video" inManagedObjectContext:_managedObjectContext];
    [request setEntity:video];
    NSSortDescriptor *sortDescriptor =
    [[NSSortDescriptor alloc] initWithKey:@"date_recorded"
                                ascending:YES
                                 selector:@selector(localizedCaseInsensitiveCompare:)];
    NSSortDescriptor *sortDescriptor2 =
    [[NSSortDescriptor alloc] initWithKey:@"title"
                                ascending:YES
                                 selector:@selector(localizedCaseInsensitiveCompare:)];
    NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor, sortDescriptor2, nil];
    [request setSortDescriptors:sortDescriptors];

    NSError *error = nil;
    NSMutableArray *mutableFetchResults = [[_managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    if (mutableFetchResults == nil) {
        //handle error
    }
    [self setVideoArray:mutableFetchResults];
    [self.tableView reloadData];

}
但是,这是表格数据的显示顺序:

Cell 1: Title @"Z", date recorded: @"December 14, 2013"
Cell 2: Title @"A", date recorded: @"December 21, 2013"
Cell 3: Title @"B", date recorded: @"December 7, 2013"
Cell 4: Title @"T", date recorded: @"December 8, 2013"

我做错什么了吗?知道为什么会这样吗?如果我做得对,它可能是CellForRowatineXpath中的某个东西吗?

将它们作为字符串排序,因此顺序是这样的。如果记录的
date\u
字段类型是一个字符串,我认为通过核心数据(如果有的话)按字符串字段的日期值进行查询是不容易的

您可以考虑将字段类型更改为DATE类型,或者在查询了来自核心数据的所有数据之后,订购数据记录。