gpt4 book ai didi

iphone - NSPredicate - 未按预期工作

转载 作者:行者123 更新时间:2023-12-03 19:32:19 28 4
gpt4 key购买 nike

我有以下代码:

    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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com