当有超过2个对象时,NSArray会崩溃应用程序

时间:2012-12-27 00:51:50

标签: objective-c nsarray nsdictionary nsnotificationcenter

所以我一直在使用Xcode中的NSNotificationCenter,我尝试使用NSDictionaryuserInfo附加到我的通知中。

NSArray *objects = [NSArray arrayWithObjects:@"Example Name", @"Example Description", @"Example Date", nil];
NSArray *keys = [NSArray arrayWithObjects:@"name", @"description", @"date", nil];
    NSDictionary *dict = [NSDictionary 
                               dictionaryWithObjects:objects 
                               forKeys:keys];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Notification" object:nil userInfo:dict];

当我尝试运行应用程序并发布通知时,它会在以下行崩溃:

NSArray *keys = [NSArray arrayWithObjects:@"name", @"description", @"date", nil];

我后来发现如果数组大小超过2个对象,应用程序就会崩溃。

因此,如果我将代码更改为下面的代码段,那么它就可以了。

NSArray *objects = [NSArray arrayWithObjects:@"Example Name", @"Example Description", nil];
NSArray *keys = [NSArray arrayWithObjects:@"name", @"description", nil];
NSDictionary *dict = [NSDictionary 
                                   dictionaryWithObjects:objects 
                                   forKeys:keys];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"Notification" object:nil userInfo:dict];

有什么方法可以解决这个问题,还是我做了一件非常糟糕的事情?

1 个答案:

答案 0 :(得分:2)

这段代码是否编译?尝试清理和重建项目。