Core Data Integer changing value?

戏子无情 提交于 2020-01-04 13:26:28

问题


i have a problem with core data storing integer (i chose int16 because they have a maximum of 6 signs).

my model holds

Entity: 'Expense'
the attribute in question is:


@property (nonatomic, retain) NSNumber * month;

it was automatically implemented as NSNumber by Xcode (Editor > createManagedmodelsubclass)

month holds an short identifier for every month. example

201203     //would be march of 2012

i store new entities with this snippet:

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

which works just fine. monthNumber has always the right value before i store it.

i retrieve objects with a fetching method and store them in an array called allExpenses. the array count is true and i have the right amount of entities in it.

now i do this:

NSMutableArray *thisMonthExpenses = [[NSMutableArray alloc]init ];

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

    }
}

to filter out the right entities that belong to the current month.

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

but somehow the code:

int tempMonth = [[entity month]intValue];

does not return 201203, but strangly 4595 (always the same value).

the same happens with this code:

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

i seem to be missing something - but i cannot figure out what, i tried around for 2 hours now and always get the wrong int value after reading my entities..

any ideas?


回答1:


201203 is 0x311F3, while 4595 is 0x11F3 — so what's happening is, you're losing the most significant byte. It sounds like in CoreData you have the number set as a 16-bit integer, which isn't capable of storing the number you want (16 bits can only represent up to the low five digits in decimal). It should be a 32-bit integer.




回答2:


have you tried

Expense.month = monthNumber;

or

[Expense setMonth:monthNumber];


来源:https://stackoverflow.com/questions/9610592/core-data-integer-changing-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!