如何检查NSNumber对象是否为空或为空?
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) //得到一个空值 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永远不会调用 NSLog(@It is empty!像这样NSLog不会被调用。 myEmptyValue 不是 nil ,而不是 NSNull 。那么它包含一个任意数字?
解决方案NSValue , NSNumber ,...应该从一个值创建并始终保持一个。测试 0 等特定值只有在不在您使用的有效值范围内时才有效。
在极少数情况下,如果代码具有代表无效或未设置的值,代码更为直接, t使用 nil (例如使用标准容器),您可以使用 NSNull 。 p>
在您的第一个示例中,可以是:
[dict setValue:[NSNull null] forKey:@emptyValue]; if([dict objectForKey:@emptyValue] == [NSNull null]){ // ... } $ c $但是注意,你可以简单地不插入(或删除)该值,除非你需要区分 nil (即不在容器中),例如无效: if ([dict objectForKey:@nonExistent] == nil){ // ... }对于第二个示例, -intValue 为您提供 0 - 但是因为发送消息到 nil 返回 0 。您还可以获得 0 。对于 intValue 设置为 0 之前的 NSNumber 如上所述,如果 0 不是有效值,则只能执行类似的操作为你。
:选项#1:
如果不需要所有值从数字范围,您可以使用一个( 0 或 -1 或...)和 -intValue / ...以具体表示空。这显然不适合你。
选项#2:
: // add if not empty: [dict setObject:someNumber forKey:someKey]; // remove if empty: [dict removeObjectForKey:someKey]; //检索数字: NSNumber * num = [dict objectForKey:someKey]; if(num == nil){ // ...不在字典中,代表空} else { // ...不为空}
然而,这意味着空的键和永远不存在的键之间没有区别
在某些罕见的情况下更方便地将所有键保存在字典中,并用不同的值表示空。如果你不能使用一个从数字范围,我们必须把不同的东西在 NSNumber 没有一个概念空 。对于这种情况,Cocoa已经有 NSNull :
//设置为number如果不为空: [dict setObject:someNumber forKey:someKey]; //如果为空,设置为NSNull: [dict setObject:[NSNull null] forKey:someKey]; //检索数字: id obj = [dict objectForKey:someKey]; if(obj == [NSNumber null]){ // ...空} else { // ...不为空 NSNumber * num = obj; // ... }此选项现在允许您区分之间 (例如非法键)
$ b $ bHow do I check if a NSNumber object is nil or empty?
OK nil is easy:
NSNumber *myNumber; if (myNumber == nil) doSomethingBut if the object has been created, but there is no value in it because an assignment failed, how can I check this? Use something like this?
if ([myNumber intValue]==0) doSomethingIs there a general method for testing objects on emptiness like for NSString available (see this post)?
Example 1
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; [dict setValue:@"" forKey:@"emptyValue"]; NSNumber *emptyNumber = [dict objectForKey:@"emptyValue"];Which value does emptyNumber contain? How can I check if emptyNumber is empty?
Example 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;What happens if I use this after emptyNumber was set to nil?
[emptyNumber intValue]Do I get zero?
Example 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!");Like this way NSLog is never called. myEmptyValue is not nil and not NSNull. So it contains an arbitrary number?
解决方案NSValue, NSNumber, ... are supposed to be created from a value and to always hold one. Testing for a specific value like 0 only works if it isn't in the range of valid values you are working with.
In the rare case where code is more straight-forward to work with if you have a value that represents "invalid" or "not set" and you can't use nil (e.g. with the standard containers) you can use NSNull instead.
In your first example this could be:
[dict setValue:[NSNull null] forKey:@"emptyValue"]; if ([dict objectForKey:@"emptyValue"] == [NSNull null]) { // ... }But note that you can simply not insert (or remove) that value unless you need to differentiate nil (i.e. not in the container) and, say, "invalid":
if ([dict objectForKey:@"nonExistent"] == nil) { // ... }As for the second example, -intValue gives you 0 - but simply because sending messages to nil returns 0. You could also get 0 e.g. for a NSNumber whose intValue was set to 0 before, which could be a valid value. As i already wrote above, you can only do something like this if 0 is not a valid value for you. Note the for you, what works best completely depends on what your requirements are.
Let me try to summarize:
Option #1:
If you don't need all values from the numbers range, you could use one (0 or -1 or ...) and -intValue / ... to specifically represent "empty". This is apparently not the case for you.
Option #2:
You simply don't store or remove the values from the container if they are "empty":
// 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 }This however means that there is no difference between keys that are empty and keys that never exist or are illegal.
Option #3:
In some rare cases its more convenient to keep all keys in the dictionary and represent "empty" with a different value. If you can't use one from the number range we have to put something differently in as NSNumber doesn't have a concept of "empty". Cocoa already has NSNull for such cases:
// 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; // ... }This option now allows you to differentiate between "empty", "not empty" and "not in the container" (e.g. illegal key).
更多推荐
检查NSNumber是否为空
发布评论