Andrei Filip
Andrei Filip

Reputation: 1143

NSPredicate and custom NSObject

I have a custom NSObject which I need to filter. I have been trying to user NSPredicate to do this, but was unable to so far. Here is my object's structure:

@interface MyBigObject : NSObject 

@property (nonatomic, strong) NSString *firstAttribute;
@property (nonatomic, strong) NSString *secondAttribute;
@property (nonatomic, strong) NSMutableArray *featuresArray;

@end

The featuresArray contains other custom objects:

typedef enum {

FeatureExists = YES, //Default
FeatureDoesNotExist = NO,
FeatureNotAvailable

} FeatureValue;

@interface MySmallObject : NSObject

@property (nonatomic, strong) NSString *title;
@property (nonatomic) FeatureValue feature;

@end

I want to only return the objects containing a MySmallObject with a certain title and which has feature == FeatureExists.

I've tried something like (and other variations) but to no success:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(featuresArray, $object, $object.title CONTAINS[c] %@ AND $object.feature = %d).@count > 0)", @"Fenced", FeatureExists];

NSLog(@"predicate = %@", predicate);

Upvotes: 1

Views: 1044

Answers (1)

Gianluca Tranchedone
Gianluca Tranchedone

Reputation: 3598

If I understood correctly, the format of your predicate is wrong. It should be [NSPredicate predicateWithFormat:@"SUBQUERY(featuresArray, $object, $object.title CONTAINS[c] %@ AND $object.feature = %d).@count > 0", @"some string", FeatureExists]; where you check in your array if any object matches the requirements.

Upvotes: 2

Related Questions