我想通过for in循环修改一些核心数据实体。获取的对象应通过谓词进行过滤。但是该循环永远不会触发,因为没有获取任何对象。

我怀疑谓词存在问题,尽管我已经仔细审查过。这是代码:

NSString *replenishString = @"Replenishment";
NSDate *now = [NSDate date];

NSPredicate *transFRCPredicate = [NSPredicate predicateWithFormat:@"(account == %@) AND ((transDate >= %@) AND (transDate <= %@)) AND (category != %@)",self.detailTransaction.account,self.detailTransaction.transDate,now,replenishString];


NSFetchedResultsController *transactionsToAdjustFRC = [WMMGTransaction MR_fetchAllGroupedBy:@"account" withPredicate:transFRCPredicate sortedBy:@"transDate" ascending:NO];

(...)

    for (WMMGTransaction *transaction in transactionsToAdjustFRC.fetchedObjects)
    {
        NSLog(@"Inside for loop--delta = %@",delta);
        transaction.category = @"Changed +";

        transaction.pointBalance = [transaction.pointBalance decimalNumberByAdding: delta];
        NSLog(@"Inside for loop--pointBalance = %@",transaction.pointBalance);
    }

有什么想法或指导吗?

谢谢!

最佳答案

在谓词中使用“!=”时要小心:nil值将不匹配。包括针对nil的特定测试:

NSPredicate *transFRCPredicate = [NSPredicate predicateWithFormat:@"(account == %@) AND ((transDate >= %@) AND (transDate <= %@)) AND (category != %@ OR category == nil)",self.detailTransaction.account,self.detailTransaction.transDate,now,replenishString];

09-27 17:27