NSPredicate因BOOL而失败

时间:2015-01-21 08:06:24

标签: ios objective-c nspredicate

我尝试过滤错过@property BOOL的对象数组 取决于此属性的值

我定义了:

NSPredicate *missedInvitationsPredicate = [NSPredicate predicateWithBlock: ^BOOL(CRGQuizInvitation* invitation, NSDictionary *bind){
    return invitation.missed == YES;
}];

但它失败了。我尝试了很多东西,比如使用predicateWithFormat,一个等号和许多其他东西。最后我通过将BOOL转换为NSNumber来解决它,但我仍然想知道它有什么问题。

1 个答案:

答案 0 :(得分:0)

我已经实现了它而没有转换为NSNumber。这是示例代码。

NSMutableArray *collection = [[NSMutableArray alloc] init];

for (int itemIndex = 0; itemIndex < 5; itemIndex++)
{
  CRGQuizInvitation *invitation = [[CRGQuizInvitation alloc] init];

  if (itemIndex %2 == 0)
  {
    invitation.missed = YES;
  }

  [collection addObject:invitation];
}

NSPredicate *missedInvitationsPredicate = [NSPredicate predicateWithBlock: ^BOOL(CRGQuizInvitation* invitation, NSDictionary *bind){
    return invitation.missed == YES;
}];

NSArray *trueValueObjCollection = [collection filteredArrayUsingPredicate: missedInvitationsPredicate];

NSLog(@"%@", trueValueObjCollection);

<强> CRGQuizInvitation

@interface CRGQuizInvitation : NSObject

@property (nonatomic, assign) BOOL missed;

@end

输出记录

<TestClass: 0x8f1d450>
<TestClass: 0x8f1e440>
<TestClass: 0x8f1e460>