gpt4 book ai didi

iphone - 如何在核心数据中使用 NSPredicate 从属性之一获取值

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:14:48 26 4
gpt4 key购买 nike

我有一个基于核心数据的项目。那里的一个属性叫做“日期”,我想从那个属性中获取值,值以字符串“2013-03”开头,但我只能得到最新值,例如字符串“2013-03-30”,如何将所有以“2013-03”开头的值放入NSArray?

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Database" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date beginsWith %@",value];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *sortDescription = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
[fetchRequest setSortDescriptors:@[sortDescription]];

最佳答案

我自己解决 NSMutableArray *array = [[NSMutableArray alloc] init];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Database" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date beginsWith %@",value];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *sortDescription = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
[fetchRequest setSortDescriptors:@[sortDescription]];

NSError *fetchError = nil;
NSArray *fetchArray = [self.managedObjectContext executeFetchRequest:fetchRequest error:&fetchError];

if ([fetchArray count] > 0)
{
for (Database *fetchResult in fetchArray)
{
[array addObject:fetchResult.date];
}
}
return array;

关于iphone - 如何在核心数据中使用 NSPredicate 从属性之一获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15720141/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com