Ali
Ali

Reputation: 2035

NSFetchRequest setpredicate question

I want to check the value entered to the predicate if empty @"" then *

I used the following code but it gives errors

could you fix it for me

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription 
                               entityForName:@"Studies" inManagedObjectContext:_context];
[fetchRequest setEntity:entity];



 [fetchRequest setPredicate:[NSPredicate predicateWithFormat:
                             @"( StudiesStudent.StudentName LIKE %@ )", ( if(StudentName != "" )  StudentName  else @"*"  )                              ]]; 

NSError *error;
self.StudentList = [_context executeFetchRequest:fetchRequest error:&error];
self.title = @"patients Result"; 
[fetchRequest release];

Upvotes: 0

Views: 1716

Answers (1)

paulbailey
paulbailey

Reputation: 5346

First and foremost, StudentName != "" isn't going to work in Objective-C. You should be using [StudentName isEqualToString:@""] if you want to see if your variable is an empty string.

Secondly, your predicate isn't going to work either, trying to use an * wildcard with LIKE. I'd suggest conditionally adding a predicate to the fetch request:

if ([StudentName isEqualToString:@""]) {
     [fetchRequest setPredicate:[NSPredicate 
       predicateWithFormat:@"(StudiesStudent.StudentName LIKE %@ )", StudentName]];
}

Upvotes: 2

Related Questions