In my model class (Beacon.h), I have this property
@property (strong, nonatomic, readonly) NSUUID *uuid;
I have an array that contains object of that Beacon class and I want to filter it using NSPredicate. It works if the uuid type is a string:
@property (strong, nonatomic, readonly) NSString *uuid;
// ..
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"uuid ==[c] %@ ",strUUID];
NSArray *filterArray = [self.arrBeacons filteredArrayUsingPredicate:predicate];
How do I write NSPredicate when the uuid property is an NSUUID (not an NSString).
Swift style predicate for matching UUID, where identifier is the name of your UUID property (works with Core Data):
NSPredicate(format: "identifier == %@", uuid as CVarArg)
I think the following predicate should work:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"uuid.UUIDString ==[c] %@ ",strUUID];
Here is a working version just for you. To prove it I performed the following steps:
The sample class:
@interface TestObj : NSObject
@property (strong, nonatomic, readonly) NSUUID *uuid;
@end
@implementation TestObj
- (NSUUID*) uuid{
return [[UIDevice currentDevice] identifierForVendor]; }
+ (void) testPredicateJustForYou{
int uuidMaxNumber = 10;
NSMutableArray* uuids = [NSMutableArray array];
for (int i = 0; i < uuidMaxNumber; i++) {
[uuids addObject: [TestObj new]];
}
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"uuid.UUIDString ==[c] %@ ",[[UIDevice currentDevice] identifierForVendor].UUIDString];
NSArray *filterArray=[uuids filteredArrayUsingPredicate:predicate];
NSAssert(filterArray.count == uuidMaxNumber, @"The predicate should work...");
}
@end
Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With