4
我正在使用CoreData的应用程序。有位置实体拥有经度和纬度值。我想要获取按用户位置排序的实体。 我尝试设置排序描述符为距离公式sqrt((x1 - x2)^ 2 +(y1 - y2)^ 2),但失败,异常情况为“... keypath ...未在实体中找到”。如何获取按计算值排序的托管对象
NSString *distanceFormula = [NSString stringWithFormat:@"sqrt(((latitude - %f) * (latitude - %f)) + ((longitude - %f) * (longitude - %f)))",
location.coordinate.latitude,
location.coordinate.latitude,
location.coordinate.longitude,
location.coordinate.longitude];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:distanceFormula ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSError *error;
NSArray *result = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
我想获取已排序的对象,而不是获取它们,然后在代码中进行排序。
任何提示赞赏。
你可以张贴反正很长的距离公式?先谢谢你。 – 2010-05-07 12:51:00
谢谢你不可饶恕。我在答案中加入了公式。 – mohsenr 2010-05-08 10:00:21