开发者

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];


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]];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜