NSPredicate to test for NULL, and blank strings

前端 未结 4 1597
借酒劲吻你
借酒劲吻你 2020-12-07 18:15

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:

N         


        
相关标签:
4条回答
  • 2020-12-07 18:52

    I think this should work:

    NSPredicate *predicateName = [NSPredicate predicateWithFormat:@"name!=nil AND name!=''"]; 
    
    0 讨论(0)
  • 2020-12-07 19:04

    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.

    0 讨论(0)
  • 2020-12-07 19:04
     NSPredicate *predicateName = [NSPredicate predicateWithFormat:@"name!=NULL"];
    
    0 讨论(0)
  • 2020-12-07 19:09

    This predicate worked for me:

    [NSPredicate predicateWithFormat:@"(%K== nil) OR %K.length == 0", @"imageUrl", @"imageUrl"]
    
    0 讨论(0)
提交回复
热议问题