我尝试过滤错过@property BOOL
的对象数组
取决于此属性的值
我定义了:
NSPredicate *missedInvitationsPredicate = [NSPredicate predicateWithBlock: ^BOOL(CRGQuizInvitation* invitation, NSDictionary *bind){
return invitation.missed == YES;
}];
但它失败了。我尝试了很多东西,比如使用predicateWithFormat,一个等号和许多其他东西。最后我通过将BOOL
转换为NSNumber
来解决它,但我仍然想知道它有什么问题。
答案 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>