我有一个 NSArray
并且需要过滤掉任何为 null 或者更确切地说具有 ' ' (空字符串)的字符串。我怎么做?我试过这样做:
NSPredicate *predicateName = [NSPredicate predicateWithFormat:@"(name!=nil)"];
但这似乎不起作用。或者也许确实如此,但有不同种类的 null...
最佳答案
如果你不使用 Core Data,你可以这样做:
NSPredicate *predicateName = [NSPredicate predicateWithFormat:@"name.length > 0"];
如果字符串为空,这将失败(因为 0 == 0
)。同样,如果name
为nil
,也会失败,因为[nil length] == 0
。
https://stackoverflow.com/questions/7369390/
相关文章:
objective-c - 从 block 内调用 [self methodName]?
objective-c - 有什么方法可以加粗 NSString 的一部分?
ios - "interfaceOrientation"在 iOS 8 中已弃用,如何更改此方法 O
iphone - 如何使用 NSNotificationcenter 的 object 属性
objective-c - 为什么 nil/NULL block 在运行时会导致总线错误?
objective-c - 删除 NSString 的最后一个字符