NSPredicate测试NULL和空白字符串 [英] NSPredicate to test for NULL, and blank strings

查看:486
本文介绍了NSPredicate测试NULL和空白字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSArray,需要过滤掉所有为null或具有''(空字符串)的字符串.我怎么做?我已经尝试过:

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)"]; 

但是似乎不起作用.也许可以,但是有不同类型的null ...

but that doesn't seem to work. Or maybe it does but there are different kinds of null...

推荐答案

如果您不使用Core Data,则可以执行以下操作:

If you don't use Core Data, you could do:

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

如果字符串为空,则会失败(因为0 == 0).同样,如果namenil,则也会失败,因为[nil 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.

这篇关于NSPredicate测试NULL和空白字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆