我已经制定了下面的代码:NSPredicate - 工作不正常
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];
正如你可以看到,我想从我的数据库,与98 waypoint_map_id什么选择特定的对象,但NSPredicate没有按照我的预期工作。零对象正在被选中。
任何任何想法?
谢谢,waypoint_map_id是数据库中的一个字符串。 – Stephen 2010-07-22 15:21:46
哦,那你确定你有一个'WayPoint'对象,其'waypoint_map_id'属性是“98”吗?您可以尝试注释谓词并转储所有要检查的“WapPoint”对象。 – TechZen 2010-07-22 17:31:24
另外,注释掉的代码看起来很奇怪。返回数组中的第一个和第三个元素将是'WayPoint' NSManagedObjects(或一个子类)。由于您没有分配给抓取的排序描述符,它们将以随机顺序排列。然后,你将他们的地址指定为整数的指针,然后尝试在坐标定义中将这些地址用作双精度。这是行不通的。我认为你可能对你的对象有些困惑。 – TechZen 2010-07-22 17:32:35