NSPredicate - 未按预期工作

Ste*_*hen 3 iphone core-data

我有以下代码:

    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];
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我正在尝试从我的数据库中选择特定对象,其中waypoint_map_id为98,但NSPredicate并不像我预期的那样工作.零对象正在被选中.

有人有什么想法?

Tec*_*Zen 6

格式的谓词不会将字符串"98"转换为数字.相反它

waypoint_map_id == "98"
Run Code Online (Sandbox Code Playgroud)

...正在寻找字符串属性.将谓词更改为:

NSInteger mapIdx=98;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"waypoint_map_id==%d", mapIDx];
Run Code Online (Sandbox Code Playgroud)

...返回谓词:

waypoint_map_id == 98
Run Code Online (Sandbox Code Playgroud)