我有一个需要过滤的自定义 NSObject。我一直在尝试使用 NSPredicate 来执行此操作,但到目前为止无法做到。这是我的对象的结构:
@interface MyBigObject : NSObject
@property (nonatomic, strong) NSString *firstAttribute;
@property (nonatomic, strong) NSString *secondAttribute;
@property (nonatomic, strong) NSMutableArray *featuresArray;
@end
featuresArray 包含其他自定义对象:
typedef enum {
FeatureExists = YES, //Default
FeatureDoesNotExist = NO,
FeatureNotAvailable
} FeatureValue;
@interface MySmallObject : NSObject
@property (nonatomic, strong) NSString *title;
@property (nonatomic) FeatureValue feature;
@end
我只想返回包含具有特定标题且具有 feature == FeatureExists 的 MySmallObject 的对象。
我试过类似的东西(和其他变体)但没有成功:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(featuresArray, $object, $object.title CONTAINS[c] %@ AND $object.feature = %d).@count > 0)", @"Fenced", FeatureExists];
NSLog(@"predicate = %@", predicate);
最佳答案
如果我没理解错的话,你的谓词格式是错误的。它应该是 [NSPredicate predicateWithFormat:@"SUBQUERY(featuresArray, $object, $object.title CONTAINS[c] %@ AND $object.feature = %d).@count > 0", @"some string", FeatureExists];
如果有任何对象符合要求,您可以在其中检查数组。
关于ios - NSPredicate 和自定义 NSObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17215234/