Reputation: 7149
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...
Upvotes: 78
Views: 64128
Reputation: 243146
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
.
Upvotes: 153
Reputation: 183
NSPredicate *predicateName = [NSPredicate predicateWithFormat:@"name!=NULL"];
Upvotes: 14
Reputation: 2464
This predicate worked for me:
[NSPredicate predicateWithFormat:@"(%K== nil) OR %K.length == 0", @"imageUrl", @"imageUrl"]
Upvotes: 7
Reputation: 5225
I think this should work:
NSPredicate *predicateName = [NSPredicate predicateWithFormat:@"name!=nil AND name!=''"];
Upvotes: 90