-->

NSPredicate使用功能不工作(NSPredicate With FUNCTION Not W

2019-07-03 16:16发布

我断言不断崩溃我的应用程序的消息不支持的函数表达式FUNCTION(SELF, "filterDistanceWithLatitude:" , latitude, longitude )。 有谁知道如何解决这一问题?

- (void)setUpFetchedResultsController
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"NextTime"]; //Retrieve data for the place entity
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]]; //How to sort it
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.context sectionNameKeyPath:nil cacheName:nil]; //Puts the data in a NSFetchedResultsController which is oddly located in CoreDataTableViewController //Puts the data in a NSFetchedResultsController which is oddly located in CoreDataTableViewController
    self.filtered = self.fetchedResultsController.fetchedObjects;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"FUNCTION(self, 'filterByDistanceWithLatitude:', latitude, longtitude) > 20"];
    self.filtered = [self.filtered filteredArrayUsingPredicate:predicate];
}

- (double)filterByDistanceWithLatitude:(NSNumber *)latitude andLongitude:(NSNumber *)longitude
{
    CLLocationDegrees latitudeCoor = [latitude doubleValue]; //Puts the latitude into a NextTime object.
    CLLocationDegrees longitudeCoor = [longitude doubleValue]; //Puts the longtitude into a NextTime object.

    CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:latitudeCoor longitude:longitudeCoor];
    CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:self.currentLocation.latitude longitude:self.currentLocation.longitude];

    NSNumber *distance = [[NSNumber alloc] initWithDouble:[loc1 distanceFromLocation:loc2]];

    return [distance doubleValue];
}

Answer 1:

甲取为(基于SQL)请求核心数据存储不能使用Objective-C的基于谓词或排序描述符。 您只能存储在数据库中的属性过滤。

这里是从“核心数据编程指南”的相关文件:

  • 获取管理对象 :

你不能提取使用基于瞬态特性谓词(虽然你可以使用瞬态特性在记录自己进行筛选)。 ......总之,不过,如果你执行一个直接获取数据,通常不应添加目标为基础的C-谓词或排序描述符的获取请求。 相反,你应该运用这些的获取结果。

  • 取谓词和排序描述符 :

有获取和存储的类型之间的一些互动。 ...在SQL店,在另一方面,编译谓词和排序描述符SQL和评估结果在数据库本身。 这主要是做了性能,但它意味着,评估发生在非可可的环境,所以那种依靠可可不能工作描述(或谓词)。



Answer 2:

'filterDistanceWithLatitude:' 大概应该是 'filterByDistanceWithLatitude:'



文章来源: NSPredicate With FUNCTION Not Working