Objective c 当应用程序有2个以上对象时,NSArray会崩溃
因此,我一直在Xcode中使用Objective c 当应用程序有2个以上对象时,NSArray会崩溃,objective-c,nsarray,nsdictionary,nsnotificationcenter,Objective C,Nsarray,Nsdictionary,Nsnotificationcenter,因此,我一直在Xcode中使用NSNotificationCenter,并尝试使用userInfo将NSDictionary附加到我的通知中 NSArray *objects = [NSArray arrayWithObjects:@"Example Name", @"Example Description", @"Example Date", nil]; NSArray *keys = [NSArray arrayWithObjects:@"name", @"description", @"d
NSNotificationCenter
,并尝试使用userInfo
将NSDictionary
附加到我的通知中
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];
有没有办法解决这个问题,或者我是不是做错了什么?这段代码可以编译吗?尝试清理和重建项目。是否启用了ARC?顺便说一句,在发布通知之前,您缺少一个括号,这只是一个复制粘贴错误?它应该可以工作-您确定您使用的对象是有效的吗?该示例使用字符串自由符,因此它们应该是有效的对象。当您遇到崩溃时,您是否遇到异常?还是EXC_坏访问?堆栈跟踪是什么样子的?啊。我讨厌发生这种事。。Xcode再次启动。我重新启动了我的电脑,它工作了。谢谢你的帮助!是的。正如我所说,当我从每个arrayClose XCode中删除第三个对象并重新启动它时,它就会工作。那是巫毒,如果你反对的话。让我知道原因。这是免费的。是的,这个建议实际上是有效的。