简体   繁体   English

NSPredicate测试NULL和空字符串

[英]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). 我有一个NSArray ,需要过滤掉任何null的字符串,或者更确切地说,有''(空字符串)。 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... 或许它确实如此,但有不同种类的空...

If you don't use Core Data, you could do: 如果您不使用Core Data,您可以:

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

If the string is empty, this will fail (because 0 == 0 ). 如果字符串为空,则会失败(因为0 == 0 )。 Similarly, if name is nil , it will also fail, because [nil length] == 0 . 同样,如果namenil ,它也会失败,因为[nil length] == 0

我认为这应该有效:

NSPredicate *predicateName = [NSPredicate predicateWithFormat:@"name!=nil AND name!=''"]; 
 NSPredicate *predicateName = [NSPredicate predicateWithFormat:@"name!=NULL"];

这个谓词对我有用:

[NSPredicate predicateWithFormat:@"(%K== nil) OR %K.length == 0", @"imageUrl", @"imageUrl"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM