我有以下代码:
NSString *mapIDx = @"98";
NSLog(@"map id: %@", mapIDx);
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"WayPoint" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
//NSPredicate *predicate = [NSPredicate predicateWithFormat:@"waypoint_map_id=%@", mapIDx];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"waypoint_map_id==%@", mapIDx];
[request setPredicate:predicate];
NSError *error;
listArray = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
[request release];
int arrayItemQuantity = [listArray count];
NSLog(@"Array Quantity: %d", arrayItemQuantity);
// Loop through the array and display the contents.
int i;
for (i = 0; i < arrayItemQuantity; i++)
{
NSLog (@"Element %i = %@", i, [listArray objectAtIndex: i]);
}
/*
NSInteger *xCoordinate = listArray[1];
NSInteger *yCoordinate = listArray[3];
NSLog(@"xCoordinate: %@", xCoordinate);
NSLog(@"yCoordinate: %@", yCoordinate);
CLLocationCoordinate2D coordinate = {xCoordinate, yCoordinate};
MapPin *pin = [[MapPin alloc]initwithCoordinates:coordinate];
[self.mapView addAnnotation:pin];
[pin release];
*/
[listArray release];
正如您所看到的,我正在尝试从我的数据库中选择特定对象,任何 waypoint_map_id 为 98 的对象,但 NSPredicate 没有按我预期的那样工作。零对象被选中。
有人有什么想法吗?
最佳答案
带格式的谓词不会将字符串“98”转换为数字。相反,它确实
waypoint_map_id == "98"
...正在寻找字符串属性。将谓词更改为:
NSInteger mapIdx=98;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"waypoint_map_id==%d", mapIDx];
...返回谓词:
waypoint_map_id == 98
关于iphone - NSPredicate - 未按预期工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3307406/