核心数据整数变化值?

时间:2012-03-07 23:11:39

标签: objective-c ios core-data

我的核心数据存储整数存在问题(我选择了int16,因为它们最多有6个符号)。

我的模特持有

Entity: 'Expense'
the attribute in question is:


@property (nonatomic, retain) NSNumber * month;

它被Xcode自动实现为NSNumber(编辑器> createManagedmodelsubclass)

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

201203     //would be march of 2012

我使用此代码段存储新实体:

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

效果很好。 monthNumber在存储之前总是正确的值。

我使用抓取方法检索对象并将它们存储在名为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值..

任何想法?

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:0)

你试过吗

Expense.month = monthNumber;

[Expense setMonth:monthNumber];