1

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

4

1 回答 1

5

(null)是当您使用带值NSLog()的格式说明符时打印的字符串。是发送到单例(您可以通过 访问)的结果。%@nil<null>descriptionNSNull[NSNull null]

NSNullNSArray在 Cocoa 集合 (和)中用作“无对象”占位符,NSDictionary因为它们不能包含nil.

这两个描述字符串令人困惑地相似,有人可能会争辩说NSNull应该针对它提交一个错误以使其更加清晰。

于 2012-07-02T04:01:39.197 回答