问题
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:
- The contents of an actual NSString
- (null)
- <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