ios - NSPredicate 用于测试 NULL 和空白字符串

我有一个 NSArray 并且需要过滤掉任何为 null 或者更确切地说具有 ' ' (空字符串)的字符串。我怎么做?我试过这样做:

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

但这似乎不起作用。或者也许确实如此,但有不同种类的 null...

最佳答案

如果你不使用 Core Data,你可以这样做:

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

如果字符串为空,这将失败(因为 0 == 0)。同样,如果namenil,也会失败,因为[nil length] == 0

https://stackoverflow.com/questions/7369390/

相关文章:

objective-c - 从 block 内调用 [self methodName]?

objective-c - 有什么方法可以加粗 NSString 的一部分?

ios - 找不到符号 : kUTTypeImage

ios - "interfaceOrientation"在 iOS 8 中已弃用,如何更改此方法 O

iphone - 如何使用 NSNotificationcenter 的 object 属性

objective-c - 为什么 nil/NULL block 在运行时会导致总线错误?

ios - 如何检查是否为 64 位构建了静态库?

objective-c - 删除 NSString 的最后一个字符

objective-c - 是否可以使用 sharedHTTPCookieStorage 为 UIW

ios - 如何调用 iPhone map 以获取当前位置作为起始地址的方向