IOS Core Data how to correctly compare string text using predicates?

It literally drives me crazy.

I have 2 objects that use NSStrings as a unique attribute.

What is the correct way to create a predicate that compares NSStrings?

Currently I have: [Challenger NSPredicateWithFormat: @ "unique =% @", uniqueValue];

I have a feeling that this compares the pointer addresses, not the actual values โ€‹โ€‹of the strings, but I cannot confirm this. I need to return yes to match string exactly.

-(BOOL)uniqueEntityExistsWithEnityName:(NSString*)entityName UniqueKey:(NSString*) uniqueKey UniqueValue:(NSString*)uniqueValue SortAttribute:(NSString*)sortDescriptorAttribute ManagedObjectContext:(NSManagedObjectContext*) context; { BOOL returnValue = NO; NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName]; //what is the correct predates to compare the text an string core data property against a passed in string? request.predicate = [NSPredicate predicateWithFormat:@"unique= %@", uniqueValue]; NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:sortDescriptorAttribute ascending:YES]; request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; NSError *error = nil; NSArray *matches = [context executeFetchRequest:request error:&error]; if (!matches) { NSLog(@"Error: no object matches"); } else if([matches count] > 1) { NSLog(@"Error: More than one object for unique record"); returnValue = YES; } else if ([matches count] == 0) { returnValue = NO; } else { returnValue = YES; } return returnValue; } 
+6
source share
2 answers

One equal sign is not even a comparator in terms of coding.

I am going to assume that the unique attribute is NSManagedObject.

 [NSPredicate predicateWithFormat:@"unique LIKE %@", uniqueValue]; 

Please note that this is case sensitive. If you want to make it insensitive, you can put [c] after LIKE.

+9
source

I do not see a problem with your predicate. Single = perfect if you want to match exact strings. If you don't need a wildcard, you don't need a slow LIKE. ( Predicate format string syntax )

However, there is a problem in your code, and this can lead to incorrect assumptions. Your if / then / else or at least the first message is incorrect. If you retrieve, it does not return an array, this means that the selection was not completed, this does not mean that the selection did not return objects.

It should be something like this:

 if (!matches) { NSLog(@"Error: couldn't execute fetch request %@", error); } else if([matches count] > 1) { NSLog(@"Error: More than one object for unique record"); returnValue = YES; } else if ([matches count] == 0) { NSLog(@"couldn't match objects"); returnValue = NO; } else { // [matches count] == 1 NSLog(@"matched one object"); returnValue = YES; } 

Oh, and I would change the order of the conditions. In my opinion, such a structure as (! Matches), ([matches count] == โ€‹โ€‹1), ([count count] == โ€‹โ€‹0), (else) makes more sense and is easier to read. You put the most important (because this is what you really want) in the last "anonymous".

+7
source

Source: https://habr.com/ru/post/911418/


All Articles