Im делает предикат для извлечения всей транзакции в определенном диапазоне дат в зависимости от типа транзакции.NSPredicate: Unable To Parse String
Вот предикат:
NSString *predicate = [NSString stringWithFormat:@"(date >= %@ AND date < %@) AND (%@)", from, to, defaultType];
Вот полный код:
- (NSArray *)transactionFromMonth:(NSInteger)fromMonth toMonth:(NSInteger)toMonth ofTypes:(NSArray *)transactionTypes
{
// get the current calendar of the device
NSCalendar *calendar = [NSCalendar currentCalendar];
// create date components using current calendar
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay
| NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
// get year
NSInteger currentYear = [components year];
// create fromDate (The first day of the month) using the current year, passing month, and 1 as the day
// to form the starting day of the month
// e.g when toMonth has a value of 1, then the resulting
// date will be January 1, (current year of the device's calendar)
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:1];
[comps setMonth:fromMonth];
[comps setYear:currentYear];
NSDate *from = [[NSCalendar currentCalendar] dateFromComponents:comps];
// create toDate
//
// Set your month here
[comps setMonth:toMonth];
// get the number of days of "to" month
NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit
inUnit:NSMonthCalendarUnit
forDate:[calendar dateFromComponents:comps]];
[comps setDay:range.length+1];
NSDate *to = [[NSCalendar currentCalendar] dateFromComponents:comps];
NSLog(@"to Months %@", to);
NSString *defaultType = [NSString stringWithFormat:@"type = %d", [[transactionTypes objectAtIndex:0] integerValue]];
NSMutableString *types = [[NSMutableString alloc] initWithString: defaultType ];
for (int i = 1; i < transactionTypes.count; i++)
{
[defaultType stringByAppendingFormat:@" OR type = %d", [[transactionTypes objectAtIndex:i] integerValue] ];
}
// here is the predicate causing the error
//
//
NSString *predicate = [NSString stringWithFormat:@"(date >= %@ AND date < %@) AND (%@)", from, to, defaultType];
return [self.transaction filteredSetUsingPredicate: [NSPredicate predicateWithFormat:predicate]].allObjects;
}
Вот декларация транзакционной модели: Примечание: .h и .m из приведенных ниже моделей автогенерируемая.
Вот пример вызова:
// I want to retrieve all transaction from January 1 to January 31 in device's current calendar year of type kTransactionTypeAddFund (which has the value of 0)
[self transactionFromMonth:1 toMonth:1 ofTypes:@[ @(kTransactionTypeAddFund) ]];
Результат:
причина: «Невозможно разобрать строку формата«(дата> = 2014-12-31 16:00:00 +0000 AND date < 2015-01-31 16:00:00 +0000) AND (type = 0) "'
На первый взгляд (я не испытывая это, и я не использую NSPredicate в довольно долгое время), я бы сказал, что вы либо выход эти даты как целые числа или вам нужно цитировать их, но я m not sure –
Добавление котировок на сегодняшний день будет относиться к ним как к NSString. Поэтому сравнение даты было бы невозможно. – Hokage
Какой _is_ ваш предикат? Вы указали сообщение об ошибке, но не код. Покажите, что вы делаете, пожалуйста. – matt