简体   繁体   中英

filteredArrayUsingPredicate always returning nil NSArray?

I have fetching some items using CoreData and I want to filter those results some more using filteredArrayUsingPredicate which is giving me some issues.

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);

Which gives me this result:

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})"
)

In this instance there is only 1 result, which is not always the case so I want to filter more:

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

Which gives me this result:

result: (null) 

Not sure what I am missing here, and I am passing the proper ManufacturerID of 192 for this filtered array predicate.

What is aManufacturerID?

By the use of the %@ format specifier it should most likely be [NSNumber numberWithInt:192]

or if you don't want to wrap with an NSNumber you can change the format in the predicate from %@ to %d

NSInteger aManufacturerID = 192;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM