(null)与< null>有什么区别? [英] What's the difference between (null) vs <null>?
问题描述
在我将其范围缩小到特定方法之前,我的应用程序中出现随机崩溃.在该方法中,我希望使用 NSString
作为参数.该 NSString
有时可以为nil,在这种情况下,该方法将终止并且不会造成任何危害.
I was getting random crashes in my app until I narrowed it down to a particular method. In that method I expect an NSString
as a parameter. This NSString
can sometimes be nil in which case the method ends and no harm is done.
当我通过 NSLog(@%@",myString)
运行方法的参数时,我发现其中之一:
When I run my method's parameter through NSLog(@"%@", myString)
I found that I get one of these:
- 实际NSString的内容
- (空)
- < null>
前两个是我的方法所期望和处理的.第三个< null> 使-[NSNull length]:无法识别的选择器发送到实例0x1b2ace8
导致我的应用程序崩溃.
The first two are expected and handled by my method. The third one, <null>, crashes my app with -[NSNull length]: unrecognized selector sent to instance 0x1b2ace8
.
我已经通过检查 nil
或 isKindOfClass
找到了解决该问题的方法,但是我的问题是(空)和<之间有什么区别?null>?.
I have found a way around the problem by checking for nil
or isKindOfClass
, but my question is what is the difference between (null) and <null>?.
推荐答案
(null)
是使用格式说明符 NSLog()
打印的字符串>%@ ,其值为 nil
.< null>
是向 NSNull
单例(您通过 [NSNull null]
description 的结果>).
(null)
is the string that NSLog()
prints when you use the format specifier %@
with a nil
value. <null>
is the result of sending description
to the NSNull
singleton (which you access via [NSNull null]
).
NSNull
在可可集合( NSArray
和 NSDictionary
)中用作无对象"占位符,因为它们不能包含 nil
.
NSNull
is used as a "no object" placeholder in Cocoa collections (NSArray
and NSDictionary
) because they cannot contain nil
.
这两个描述字符串令人困惑,它们相似,并且可能会争辩说 NSNull
应该针对它提一个错误,以使其更加清楚.
The two description strings are confusingly similar, and one could argue that NSNull
should have a bug filed against it to make this a little more clear.
这篇关于(null)与< null>有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!