NSDictionary EXC_BAD_ACCESS

时间:2014-01-06 16:06:33

标签: ios objective-c exc-bad-access

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

我已尝试在乐器中使用Zombies模板,但我从未得到过对话框。在破坏点时,应用程序在initWithObjectsAndKeys上崩溃。我的死变量在哪里?

2 个答案:

答案 0 :(得分:16)

您不能将原始类型放入Dictionary。用NSNumber包裹变量total,然后将其放入Dictionary。

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];

答案 1 :(得分:-1)

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