Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I Match NSPredicate for non-NSString data type?

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).

like image 696
Sheshnath Avatar asked Nov 30 '25 07:11

Sheshnath


2 Answers

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)
like image 98
ion Avatar answered Dec 01 '25 21:12

ion


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:

  1. Create a new project (Single View App)
  2. Add the code below in the top of the AppDelegate.m class
  3. Call [TestObj testPredicateJustForYou]; in the didFinishLaunchingWithOptions: function.

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!

like image 41
Balazs Nemeth Avatar answered Dec 01 '25 19:12

Balazs Nemeth