核心数据整数变化值?

我有一个核心数据存储整数的问题(我selectint16,因为他们有最多6个标志)。

我的模型成立

Entity: 'Expense' the attribute in question is: @property (nonatomic, retain) NSNumber * month; 

它被Xcode自动实现为NSNumber(Editor> createManagedmodelsubclass)

月份每个月都有一个简短的标识符。 例

 201203 //would be march of 2012 

我用这个片段存储新的实体:

 [newExpense setValue:monthNumber forKey:@"month"]; 

这工作得很好。 monthNumber在我存储之前总是有正确的值。

我使用fetching方法检索对象,并将其存储在名为allExpenses的数组中。 数组数是真实的,我有适量的实体在里面。

现在我这样做:

 NSMutableArray *thisMonthExpenses = [[NSMutableArray alloc]init ]; for (Expense *entity in allExpenses) { int tempMonth = [[entity month]intValue]; if (tempMonth == month) { [thisMonthExpenses addObject:entity]; } } 

筛选出属于当前月份的正确实体。

 month // is an integer that holds the encoded month (again: correctly!) 

但不知何故的代码:

 int tempMonth = [[entity month]intValue]; 

不会返回201203,而是4595(总是相同的值)。

这个代码也是一样的:

 for (Expense *entity in monthExpenses) { if ([entity day].intValue == todayNumber.intValue) { //HERE ENTITY DAY.INTVALUE RETURNS A COMPLETELY WRONG INTEGER! [thisDayExpenses addObject:entity]; } } 

我似乎失去了一些东西 – 但我不明白是什么,我现在尝试了2个小时,阅读我的实体后总是得到错误的int值。

有任何想法吗?

201203是0x311F3,而4595是0x11F3 – 所以发生的是,你失去了最重要的字节。 这听起来像是在CoreData中,您设置的数字为16位整数,不能存储您想要的数字(16位只能表示最多十进制的低五位数)。 它应该是一个32位整数。

你有没有尝试过

 Expense.month = monthNumber; 

要么

 [Expense setMonth:monthNumber];