user437064
user437064

Reputation:

Filter NSMutableArray with NSPredicate

I fetch records from core data and fill NSMutableArray with them,now i want to filter them with below code but doesn't work properly.how should i do it?

[array filterUsingPredicate:[NSPredicate predicateWithFormat:@"date==%@",date1 ]];
//date = a field with format NSDate in core data
//date1 = NSDate

Upvotes: 0

Views: 727

Answers (2)

SajjadZare
SajjadZare

Reputation: 2378

Maybe it's because of that you enter date with time and when you want to filter array with date1 the time of date1 isn't equal to date that you store in core data

look at this code and enter a date without time

NSDate *dateTime=[NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSDateComponents *components = [[NSDateComponents alloc] init];
components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
                         fromDate:dateTime];
NSDate *dateOnly = [calendar dateFromComponents:components];

Upvotes: 0

LJ Wilson
LJ Wilson

Reputation: 14427

Why wouldn't you use the predicate in your NSFetchRequest and keep the NSArray populated with only the records you need?

Upvotes: 1

Related Questions