gpt4 book ai didi

ios - NSSortDescriptor 排序正值

转载 作者:行者123 更新时间:2023-11-28 20:06:39 25 4
gpt4 key购买 nike

在我的核心数据中,我要求 x 个按距离 (NSNumber[double]) 升序排序的对象。问题是它还给我返回负数。我知道这是有道理的,但我只想要正数,我该怎么做?

NSManagedObjectContext *context = generateManagedObjectContext();

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:kCORE_DATA_ALL_TRAPS_ENTITY inManagedObjectContext:context];
[fetchRequest setEntity:entity];

// Specify how the fetched objects should be sorted
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:DISTANCE_TO_CLOSE_POINT ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];

// Limit the restlus to specific number
[fetchRequest setFetchLimit:numberOfTraps];

NSError *error = nil;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil || fetchedObjects.count == 0) {
NSLog(@"%s error: %@", __PRETTY_FUNCTION__, error.localizedDescription);
return nil;
}

最佳答案

试试这个,

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"distance >= %d", 0];

[fetchRequest setPredicate:predicate];

关于ios - NSSortDescriptor 排序正值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21875056/

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