浮点数的 Core-Data 谓词过滤

发布于 2024-11-27 23:54:24 字数 2426 浏览 2 评论 0原文

基本上,我有一个 MKMapView 位置数据库,当点击每个注释上的详细信息公开按钮时,它会显示有关该位置的更多信息,例如他们的网站和电话号码。

问题是,在我的数据库中,某些坐标只有 4 或 5 位小数,而不是 6 位,因此当点击按钮时,应用程序会崩溃。 为了阻止这种情况,我在 UIAlertView 中添加了“我们无法找到更多信息” 有什么方法可以更好地使用我的谓词吗?

NSString *lat = [NSString stringWithFormat:@"%.6f", view.annotation.coordinate.latitude];
NSString *lon = [NSString stringWithFormat:@"%.6f", view.annotation.coordinate.longitude];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Stores" inManagedObjectContext:_managedObjectContext];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Latitude == %@ && Longitude == %@", lat, lon];
[request setPredicate:predicate];

NSError *error = nil;
NSArray *stores = [_managedObjectContext executeFetchRequest:request error:&error];
[request release];
if (![stores count]) {
    UIAlertView *alert = [[[UIAlertView alloc]initWithTitle:@"Error" message:@"Sorry, we are unable to locate more information for this location." delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles: nil]autorelease];
    [alert show];
 } else {



    Stores *currentStore = [stores objectAtIndex:0];
    NSLog(@"current store %@", currentStore);

    AddressAnnotation *annotation = view.annotation;

    MoreInfoTable *moreInfoView = [[MoreInfoTable alloc] initWithStyle:UITableViewStyleGrouped];
    moreInfoView.currentStore = currentStore;
    moreInfoView.managedObjectContext = self.managedObjectContext;
    moreInfoView.title = annotation.title ;
    moreInfoView.getWebsite =[NSURL URLWithString:annotation.website];
    moreInfoView.getDirections = [NSURL URLWithString:[NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f", mapView.userLocation.coordinate.latitude, mapView.userLocation.coordinate.longitude, annotation.coordinate.latitude, annotation.coordinate.longitude]];
    moreInfoView.footer = [NSString stringWithFormat:@"%.1f miles away", annotation.distanceFromLocation];
    moreInfoView.address = annotation.address;
    moreInfoView.getPhoneNumber = [NSMutableString stringWithString:annotation.phoneNumber];
    moreInfoView.lat = view.annotation.coordinate.latitude;
    moreInfoView.lon = view.annotation.coordinate.longitude;

    [self.navigationController pushViewController:moreInfoView animated:YES];
    [moreInfoView release]; 

}

}

Basically, I have a database of locations for an MKMapView, and when the detail disclosure button on each annotation is tapped, it brings up more information on the location, for example their website and phone number.

Problem is, in my database some coordinates only have 4 or 5 decimal points instead of 6, so when the button was tapped the app would crash.
To stop this I added in the UIAlertView to say "We are unable to locate more information"
Is there any way to use my predicate in a better way?

NSString *lat = [NSString stringWithFormat:@"%.6f", view.annotation.coordinate.latitude];
NSString *lon = [NSString stringWithFormat:@"%.6f", view.annotation.coordinate.longitude];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Stores" inManagedObjectContext:_managedObjectContext];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Latitude == %@ && Longitude == %@", lat, lon];
[request setPredicate:predicate];

NSError *error = nil;
NSArray *stores = [_managedObjectContext executeFetchRequest:request error:&error];
[request release];
if (![stores count]) {
    UIAlertView *alert = [[[UIAlertView alloc]initWithTitle:@"Error" message:@"Sorry, we are unable to locate more information for this location." delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles: nil]autorelease];
    [alert show];
 } else {



    Stores *currentStore = [stores objectAtIndex:0];
    NSLog(@"current store %@", currentStore);

    AddressAnnotation *annotation = view.annotation;

    MoreInfoTable *moreInfoView = [[MoreInfoTable alloc] initWithStyle:UITableViewStyleGrouped];
    moreInfoView.currentStore = currentStore;
    moreInfoView.managedObjectContext = self.managedObjectContext;
    moreInfoView.title = annotation.title ;
    moreInfoView.getWebsite =[NSURL URLWithString:annotation.website];
    moreInfoView.getDirections = [NSURL URLWithString:[NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f", mapView.userLocation.coordinate.latitude, mapView.userLocation.coordinate.longitude, annotation.coordinate.latitude, annotation.coordinate.longitude]];
    moreInfoView.footer = [NSString stringWithFormat:@"%.1f miles away", annotation.distanceFromLocation];
    moreInfoView.address = annotation.address;
    moreInfoView.getPhoneNumber = [NSMutableString stringWithString:annotation.phoneNumber];
    moreInfoView.lat = view.annotation.coordinate.latitude;
    moreInfoView.lon = view.annotation.coordinate.longitude;

    [self.navigationController pushViewController:moreInfoView animated:YES];
    [moreInfoView release]; 

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

审判长 2024-12-04 23:54:24

我假设您正在尝试获取所有具有纬度或经度的商店实体。您应该检查 Latitude 或 Longitude != nil 而不是与 6 位十进制字符串进行比较。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Latitude != nil && Longitude != nil"];
[request setPredicate:predicate];

I'm assuming you are trying to get all Stores entities which have a lat or lon. You should check Latitude or Longitude != nil instead of comparing to your 6 decimal string.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Latitude != nil && Longitude != nil"];
[request setPredicate:predicate];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文