2011-08-03 112 views
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]; 

} 

} 

回答

1

我假設你正試圖讓所有的存儲實體有經濟或經濟。你應該檢查緯度或經度!=零,而不是比較你的6位十進制字符串。

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