filteredArrayUsingPredicate总是返回nil NSArray?

时间:2012-03-18 12:20:27

标签: objective-c ios core-data

我使用CoreData获取了一些项目,我想使用filteredArrayUsingPredicate更多地过滤这些结果,这给了我一些问题。

CoreData Fetch:

NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:[NSEntityDescription entityForName:@"Collection" inManagedObjectContext:aContext]];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"CategoryName==%@", aCategoryName];
[request setPredicate:pred];

NSError *error=nil;
NSArray *tempArray=[aContext executeFetchRequest:request error:&error];
[request release], request = nil;

NSLog(@"results: %@", tempArray);

这给了我这个结果:

results: (
    "<Collection: 0x8eb1920> (entity: Collection; id: 0x8eb0aa0 <x-coredata://7C4A4A5D-691A-4F02-9450-D0D910B53903/Collection/p95> ; data: {\n    CategoryID = 22832;\n    CategoryName = \"2000 - NOURISON 2000\";\n    IsDeleted = 0;\n    ManufacturerID = 192;\n    ModifiedOn = \"2012-03-08 09:00:46 +0000\";\n    ParentCategoryID = 0;\n    PhotoName = \"\";\n    SortOrder = 0;\n})"
)

在这种情况下只有1个结果,但情况并非总是如此,所以我想过滤更多:

Collection *collection = [[tempArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"ManufacturerID==%@", aManufacturerID]]  lastObject];
NSLog(@"result: %@ " , collection);

这给了我这个结果:

result: (null) 

不确定我在这里缺少什么,并且我为此过滤后的数组谓词传递了正确的ManufacturerID 192。

1 个答案:

答案 0 :(得分:1)

什么是制造商?

通过使用%@格式说明符,它最有可能是[NSNumber numberWithInt:192]

或者如果您不想使用NSNumber打包,则可以将谓词中的格式从%@更改为%d

NSInteger aManufacturerID = 192;

Collection *collection = [[tempArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"ManufacturerID==%d", aManufacturerID]]  lastObject];
NSLog(@"result: %@ " , collection);