ios - NSPredicate 和自定义 NSObject

标签 ios nspredicate

我有一个需要过滤的自定义 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/

相关文章:

php - AFNetworking 通过 HTTPS 发出发布请求

ios - NSExpression 尊重子查询 NSPredicate

ios - Jenkins 和 CocoaPods

ios - 删除/编辑核心数据对象时遵循逻辑和关系

ios - 将 UILabel 文本对齐到左下角

ios - 如何使用 NSPredicate 通过 CoreData 中的 NSSet 属性的元素来过滤对象?

ios - 按日期过滤 hh :mm using NSPredicate

objective-c - 如何创建一个 NSPredicate 来过滤给定范围内的所有字符,包括数字/数字?

iphone - 在一对多关系 Coredata 中检索特定对象的信息

ios - stringByReplacingOccourencesOfString 与 NSMutableAttributedString