Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSPredicate to test for NULL, and blank strings

I have an NSArray and need to filter out any strings that are null or rather, have ' ' (empty string). How do I do that? I have tried doing:

NSPredicate *predicateName = [NSPredicate predicateWithFormat:@"(name!=nil)"];  

but that doesn't seem to work. Or maybe it does but there are different kinds of null...

like image 780
Doz Avatar asked Sep 10 '11 03:09

Doz


1 Answers

If you don't use Core Data, you could do:

NSPredicate *predicateName = [NSPredicate predicateWithFormat:@"name.length > 0"]; 

If the string is empty, this will fail (because 0 == 0). Similarly, if name is nil, it will also fail, because [nil length] == 0.

like image 60
Dave DeLong Avatar answered Sep 22 '22 03:09

Dave DeLong