- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如何检查 NSNumber 对象是否为 nil 或为空?
OK nil 很简单:
NSNumber *myNumber;
if (myNumber == nil)
doSomething
但是如果对象已经被创建,但是因为赋值失败而里面没有值,我该如何检查呢?使用这样的东西?
if ([myNumber intValue]==0)
doSomething
是否有像 NSString 这样的通用方法来测试对象是否为空(参见 post)?
示例 1
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:@"" forKey:@"emptyValue"];
NSNumber *emptyNumber = [dict objectForKey:@"emptyValue"];
emptyNumber
包含哪个值?如何检查 emptyNumber
是否为空?
示例 2
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:@"" forKey:@"emptyValue"];
NSString *myString = [dict objectForKey:@"emptyValue"];
if (myString == nil || [myString length] == 0)
// got an empty value
NSNumber *emptyNumber=nil;
如果我在 emptyNumber
设置为 nil 后使用它会怎样?
[emptyNumber intValue]
我会得到零吗?
示例 3
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:@"" forKey:@"emptyValue"];
NSNumber *myEmptyValue = [dict objectForKey:@"emptyValue"];
if (myEmptyValue == nil)
// NSLog is never called
NSLog(@"It is empty!");
像这样 NSLog 永远不会被调用。 myEmptyValue
不是 nil
也不是 NSNull
。所以它包含一个任意数字?
最佳答案
NSValue
, NSNumber
, ... 应该是从一个值创建的,并且总是保持一个值。测试特定值(如 0
)仅在它不在您正在使用的有效值范围内时才有效。
在极少数情况下,如果您有一个表示“无效” 或“未设置” 的值并且您可以'如果不使用 nil
(例如使用标准容器),您可以使用 NSNull
相反。
在您的第一个示例中,这可能是:
[dict setValue:[NSNull null] forKey:@"emptyValue"];
if ([dict objectForKey:@"emptyValue"] == [NSNull null]) {
// ...
}
但请注意,除非您需要区分 nil
(即不在容器中)和比方说“无效”:
if ([dict objectForKey:@"nonExistent"] == nil) {
// ...
}
至于第二个例子,-intValue
给你 0
- 但只是因为发送消息到 nil
返回 0
。你也可以获得 0
例如对于 NSNumber
,其 intValue
之前设置为 0
,这可能是一个有效值。
正如我在上面已经写过的,如果 0
对你来说不是一个有效值,你只能做这样的事情。请注意对您来说,什么最有效完全取决于您的要求。
让我尝试总结:
选项 #1:
如果您不需要数字范围内的所有值,您可以使用一个(0
或 -1
或 ...)和 -intValue
/... 来具体表示“空”。这显然不是你的情况。
选项#2:
如果容器中的值“空”,您根本不会存储或删除容器中的值:
// add if not empty:
[dict setObject:someNumber forKey:someKey];
// remove if empty:
[dict removeObjectForKey:someKey];
// retrieve number:
NSNumber *num = [dict objectForKey:someKey];
if (num == nil) {
// ... wasn't in dictionary, which represents empty
} else {
// ... not empty
}
然而,这意味着空键与从不存在或非法的键之间没有区别。
选项 #3:
在极少数情况下,将所有键保留在字典中并用不同的值表示“空”会更方便。如果您不能使用数字范围内的一个,我们必须放入不同的内容,因为 NSNumber
没有 “空” 的概念。 Cocoa 已经为这种情况提供了 NSNull
:
// set to number if not empty:
[dict setObject:someNumber forKey:someKey];
// set to NSNull if empty:
[dict setObject:[NSNull null] forKey:someKey];
// retrieve number:
id obj = [dict objectForKey:someKey];
if (obj == [NSNumber null]) {
// ... empty
} else {
// ... not empty
NSNumber *num = obj;
// ...
}
此选项现在允许您区分“空”、“非空” 和“不在容器中”(例如非法键)。
关于iphone - 检查 NSNumber 是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3716599/
我已经为使用 JGroups 编写了简单的测试。有两个像这样的简单应用程序 import org.jgroups.*; import org.jgroups.conf.ConfiguratorFact
我有一个通过 ajax 检索的 json 编码数据集。我尝试检索的一些数据点将返回 null 或空。 但是,我不希望将那些 null 或空值显示给最终用户,或传递给其他函数。 我现在正在做的是检查
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Why does one often see “null != variable” instead of “
嗨在我们公司,他们遵循与空值进行比较的严格规则。当我编码 if(variable!=null) 在代码审查中,我收到了对此的评论,将其更改为 if(null!=variable)。上面的代码对性能有影
我正在尝试使用 native Cordova QR 扫描仪插件编译项目,但是我不断收到此错误。据我了解,这是代码编写方式的问题,它向构造函数发送了错误的值,或者根本就没有找到构造函数。那么我该如何解决
我在装有 Java 1.8 的 Windows 10 上使用 Apache Nutch 1.14。我已按照 https://wiki.apache.org/nutch/NutchTutorial 中提
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: what is “=null” and “ IS NULL” Is there any difference bet
Three-EyedRaven 内网渗透初期,我们都希望可以豪无遗漏的尽最大可能打开目标内网攻击面,故,设计该工具的初衷是解决某些工具内网探测速率慢、运行卡死、服务爆破误报率高以及socks流
我想在Scala中像在Java中那样做: public void recv(String from) { recv(from, null); } public void recv(String
我正在尝试从一组图像补丁中创建一个密码本。我已将图像(Caltech 101)分成20 X 20图像块。我想为每个补丁创建一个SIFT描述符。但是对于某些图像补丁,它不返回任何描述符/关键点。我尝试使
我在验证器类中自动连接的两个服务有问题。这些服务工作正常,因为在我的 Controller 中是自动连接的。我有一个 applicationContext.xml 文件和 MyApp-servlet.
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 已关闭10 年前。 问题必须表现出对要解决的问题的最低程度的了解。告诉我们您尝试过做什么,为什么不起作用,以
大家好,我正在对数据库进行正常的选择,但是 mysql_num_rowsis 为空,我不知道为什么,我有 7 行选择。 如果您发现问题,请告诉我。 真的谢谢。 代码如下: function get_b
我想以以下格式创建一个字符串:id[]=%@&stringdata[]=%@&id[]=%@&stringdata[]=%@&id[]=%@&stringdata[]=%@&等,在for循环中,我得到
我正在尝试使用以下代码将URL转换为字符串: NSURL *urlOfOpenedFile = _service.myURLRequest.URL; NSString *fileThatWasOpen
我正在尝试将NSNumber传递到正在工作的UInt32中。然后,我试图将UInt32填充到NSData对象中。但是,这在这里变得有些时髦... 当我尝试将NSData对象中的内容写成它返回的字符串(
我正在进行身份验证并收到空 cookie。我想存储这个 cookie,但服务器没有返回给我 cookie。但响应代码是 200 ok。 httpConn.setRequestProperty(
我认为 Button bTutorial1 = (Button) findViewById(R.layout.tutorial1); bTutorial1.setOnClickListener
我的 Controller 中有这样的东西: model.attribute("hiringManagerMap",hiringManagerMap); 我正在访问此 hiringManagerMap
我想知道如何以正确的方式清空列表。在 div 中有一个列表然后清空 div 或列表更好吗? 我知道这是一个蹩脚的问题,但请帮助我理解这个 empty() 函数:) 案例)如果我运行这个脚本会发生什么:
我是一名优秀的程序员,十分优秀!