PK86
PK86

Reputation: 1198

NSPredicate predicateWithFormat for email issue

I am facing issue to create NSPredicate type object with predicateWithFormat method.

AppDelegate *appdelegateObj = (AppDelegate *)[[UIApplication sharedApplication] delegate];

self.managedObjectContext = [appdelegateObj managedObjectContext];
NSString *emailWithBackslashedAtTheRate = [email stringByReplacingOccurrencesOfString:@"@" withString:@"\\@"];

NSFetchRequest *fetchRequestObj = [NSFetchRequest fetchRequestWithEntityName:@"Usertable"];
NSMutableArray *dataList;

**NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"email CONTAINS[c] %@",emailWithBackslashedAtTheRate]];**
[fetchRequestObj setPredicate:predicate];

@synchronized(delegate.persistentStoreCoordinator) {

    NSError *error = nil;
    NSMutableArray *filteredUser = (NSMutableArray *)[self.managedObjectContext executeFetchRequest:fetchRequestObj error:&error];
}

Can anybody explain, what is issue?

Upvotes: 1

Views: 213

Answers (2)

pbasdf
pbasdf

Reputation: 21536

You cannot use the NSString method stringWithFormat to build format strings for use in a predicate. Despite the similarity in name, predicateWithFormat and stringWithFormat work differently. You should pass the correct format string directly to predicateWithFormat:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"email CONTAINS[c] %@", email];

(The key difference in this instance is that predicateWithFormat encloses the string represented by %@ in single quotes, whereas stringWithFormat does not.)

Upvotes: 0

Hitesh Boricha
Hitesh Boricha

Reputation: 114

In Your Usertable, Field Name Email Must be string.enter image description here

 AppDelegate *coreAppDelegate=[[UIApplication sharedApplication]delegate];
        NSManagedObjectContext *context=[coreAppDelegate managedObjectContext];
        NSEntityDescription *entityDecs=[NSEntityDescription entityForName:@"UserDetail" inManagedObjectContext:context];
        NSFetchRequest *request =[[NSFetchRequest alloc]init];
        [request setEntity:entityDecs];
        //  NSLog(@"username = %@",mutarr);
        NSPredicate *pred=[NSPredicate predicateWithFormat:@"(email=%@)",TxtEmail];
        [request setPredicate:pred];
        NSManagedObject *matches =nil;
        NSError *error;

Upvotes: 2

Related Questions