2011-08-03 4 views
0

基本的に、私はMKMapViewの場所のデータベースを持っており、各注釈の詳細開示ボタンをタップすると、その場所に関する詳細情報、たとえばWebサイトと電話番号が表示されます。浮動小数点のコアデータ述語フィルタリング

問題は、私のデータベースでは、いくつかの座標が6ではなく4または5の小数点しか持たないため、ボタンがタップされたときにアプリケーションがクラッシュする可能性があります。 これを停止するには、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]; 

} 

} 

答えて

1

私はあなたが緯度または経度のあるすべての店舗エンティティを取得しようとしていると仮定しています。 6桁の6桁の文字列と比較する代わりに、LatitudeまたはLongitude!= nilをチェックする必要があります。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Latitude != nil && Longitude != nil"]; 
[request setPredicate:predicate]; 
関連する問題