Predicate with CAST

Asked

Viewed 72 times

1

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

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

  • Just to understand: if it’s a decimal, why is the column type string? And why do you need to do this cast within itself predicate?

  • At the moment is being treated a latitude and longitude inside the bank as String for some other operations that will be performed later. For this reason I would have to perform this cast in "Where"

  • Yes, I understood that you need these columns as decimal to create your conditions. I just didn’t understand why not save these values already in a decimal column. To be honest, I don’t know anything similar to CAST directly on Core Data.

  • decimal? What kind of data are you referring to?

1 answer

0


It is possible to have CAST on 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 your entity’s ownership in Coredata is "latitude":

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

More examples here (in English): http://funwithobjc.tumblr.com/post/1677163679/creating-an-advanced-nspredicateeditorrowtemplate

Browser other questions tagged

You are not signed in. Login or sign up in order to post.