2010-07-22 43 views
3

我已经制定了下面的代码: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没有按照我的预期工作。零对象正在被选中。

任何任何想法?

回答

6

带格式的谓词不会将字符串“98”转换为数字。取而代之的是它

waypoint_map_id == "98" 

...这是寻找字符串属性。谓词更改为:

NSInteger mapIdx=98; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"waypoint_map_id==%d", mapIDx]; 

...返回的断言:解决

waypoint_map_id == 98 
+0

谢谢,waypoint_map_id是数据库中的一个字符串。 – Stephen 2010-07-22 15:21:46

+0

哦,那你确定你有一个'WayPoint'对象,其'waypoint_map_id'属性是“98”吗?您可以尝试注释谓词并转储所有要检查的“WapPoint”对象。 – TechZen 2010-07-22 17:31:24

+0

另外,注释掉的代码看起来很奇怪。返回数组中的第一个和第三个元素将是'WayPoint' NSManagedObjects(或一个子类)。由于您没有分配给抓取的排序描述符,它们将以随机顺序排列。然后,你将他们的地址指定为整数的指针,然后尝试在坐标定义中将这些地址用作双精度。这是行不通的。我认为你可能对你的对象有些困惑。 – TechZen 2010-07-22 17:32:35

2

假设您确实在数据库中有该对象,请尝试围绕该值添加引号?

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"waypoint_map_id==\"%@\"", mapIDx]; 

(在救命稻草!)

你prediacte看起来不错,所以我会立即开始怀疑该bug是在别处:

  • definintely是否有与该ID的航点?
  • 是listArray nil,即其他内容在请求中出错了吗?

你不检查是什么错误 - 也许这会给你更多的信息?

NSError *error = nil; 
NSArray *results = [managedObjectContext executeFetchRequest:request error:&error]; 
[request release]; 

if (nil == results || nil != error) 
    NSLog(@"Error getting results : %@", error); 

listArray = [results mutableCopy]; 

希望这对您有所帮助!

+0

感谢,我已经把错误校验码并重新测试,没有得到任何错误。有确定的航点与该ID,当我运行没有谓词,我可以在控制台中看到输出。 – Stephen 2010-07-22 15:23:10

+0

如果你运行它没有prediacte和输出__NSLog(@“%@”,item.waypoint_map_id); __ listArray中的每个项目,你会得到什么? – deanWombourne 2010-07-22 15:59:13

+0

以下行:NSLog(@“Element%i =%@”,i,[[listArray objectAtIndex:i] valueForKey:@“waypoint_map_id”]);将元素1813 = 98'输出到控制台。 – Stephen 2010-07-22 16:06:27

1

问题:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"waypoint_map_id contains[cd] %@", mapIDx]; 
    [request setPredicate:predicate];