Predicate with CAST

1

Accessing the database .sqlite via sql normally, I can use the CAST('coluna' as decimal) function.

Via NSPredicate using CoreData , how do I do the same thing? I have a column of type String and I need to make a cast to decimal .

    
asked by anonymous 28.11.2014 / 17:21

1 answer

0

It is possible to have CAST in an NSPredicate.

The syntax is:

CAST(<o que vai ser convertido>, 'classe destino')

Example:

NSArray *array = @[@"10.0", @"8.99"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"CAST(self, 'NSNumber') > %@", [NSNumber numberWithDouble:9.0]];

NSLog(@"%@", [array filteredArrayUsingPredicate:predicate]);

In your case, assuming that your entity's ownership in CoreData is "latitude":

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"CAST(latitude, 'NSNumber') > %@", [NSNumber numberWithDouble:9.0]];

More examples here: link

    
06.12.2014 / 00:49