Ios NSDictionary EXC\u访问错误

Ios NSDictionary EXC\u访问错误,ios,objective-c,exc-bad-access,Ios,Objective C,Exc Bad Access,我曾尝试在Instruments中使用Zombies模板,但从未获得对话框。中断时,应用程序在initWithObjectsAndKeys上崩溃。我的死变量在哪里?您不能将基本类型放入字典中。用NSNumber包装变量total,然后将其放入字典中 NSInteger total = 4556; // Initialize total // NSLog all fields to make sure the variables are in place NSLog(@"%@", originF

我曾尝试在Instruments中使用Zombies模板,但从未获得对话框。中断时,应用程序在initWithObjectsAndKeys上崩溃。我的死变量在哪里?

您不能将基本类型放入字典中。用
NSNumber
包装变量total,然后将其放入字典中

NSInteger total = 4556; // Initialize total

// NSLog all fields to make sure the variables are in place
NSLog(@"%@", originField.text);
NSLog(@"%@", destinationField.text);
NSLog(@"%i", [startField.text integerValue]);
NSLog(@"%i", [endField.text integerValue]);
NSLog(@"%i", total);

// Create a dictionary of them
NSDictionary *newDict = [[NSDictionary alloc] initWithObjectsAndKeys:@"Sat. Jan 6th, 2014", @"date", originField.text, @"origin", destinationField.text, @"destination", [startField.text integerValue], @"start", [endField.text integerValue], @"end", total, @"total", @"Personal", @"type", nil];
// EXC_BAD_ACCESS CODE 1

您应该在NSDictionary中插入startField和endField的原始NSSTring*值,并在必要时将其转换为int。

可以使用新的表示法
@(someNumber)
,以避免这条长线变得更长。@HotLicks同意。我仍然以传统的方式工作。我有点来回切换。在很长的一行中,简洁的符号是有帮助的,因为否则语句将变得难以理解。
NSDictionary *newDict = [[NSDictionary alloc] initWithObjectsAndKeys:@"Sat. Jan 6th, 2014", @"date", 
originField.text, @"origin", 
destinationField.text, @"destination", 
@([startField.text integerValue]), @"start", 
@([endField.text integerValue]), @"end", 
@(total), @"total", 
@"Personal", @"type", nil];