What's the difference between (null) vs <null>?

落花浮王杯 提交于 2019-12-13 01:43:44

问题


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.

When I run my method's parameter through NSLog(@"%@", myString) I found that I get one of these:

  1. The contents of an actual NSString
  2. (null)
  3. <null>

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.

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>?.


回答1:


(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 is used as a "no object" placeholder in Cocoa collections (NSArray and NSDictionary) because they cannot contain nil.

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.



来源:https://stackoverflow.com/questions/11287590/whats-the-difference-between-null-vs-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!