键 0 是否与 null 相同,如何防止它崩溃?

Is key 0 the same as null and how do I prevent it from crashing?

我在函数中有两个 variables 类型 NSMutableDictionary。该函数运行良好,但 firebase-analytics 表明它有时会崩溃。

debug-mode 中,其中一个变量的值是 0 key/value pairs。这是否意味着它完全没有价值,因此是 nil?如果是这样,我如何检查它是否为零以防止它崩溃?

函数如下:

-(void)setPerson:(NSMutableDictionary*)newPerson{
    if (thisPerson != newPerson) {

    thisPerson = newPerson;

    //It Crashes Here In The NSLog... sometimes
    NSLog(@"currentPerson: %@",newCurrentPerson);
}

所以,thisPerson 一开始是明确的 nil,newPerson0 key/value pair。然后 thisPerson 也被分配了 key 0/value pair 值。有时它会在 NSLog 中崩溃。非常混乱。

  1. In debug-mode the values of one of the variables is 0 key/value pairs. Does this mean that it is completely without value and therefore is nil?

没有。这意味着,该变量包含完全没有值的字典对象,因此它不是 nil。它是空的NSMutableDictionary *(或者如果这个函数有一些错误可能是NSDictionary *)。

  1. If so, how do I check whether it's nil to prevent it from crashing?

要检查 Objective C 中的变量是否为 nil 你应该简单地做:

if (newPerson == nil) {
    // do something with nil case
}

但这可能不是您需要的。

  1. 关于nil的更多信息:
    //It Crashes Here In The NSLog... sometimes
    NSLog(@"currentPerson: %@",newCurrentPerson);

如果您隐含地假设 NSLog(@"currentPerson: %@",nil); 会导致崩溃 - 那么这是错误的。在这种情况下,NSLog 可以很好地处理 nil

  1. 所以我认为问题出在您发布的代码中。