核心数据整数更改值? [英] Core Data Integer changing value?

查看:128
本文介绍了核心数据整数更改值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个核心数据存储整数的问题(我选择int16,因为他们有最多6个迹象)。



我的模型保存

 实体:'费用'
所讨论的属性是:


@property保留)NSNumber *月;

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



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

  201203 //将是2012年3月

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

  [newExpense setValue:monthNumber forKey :@月]; 



我通过抓取方法检索对象,并将它们存储在一个名为 allExpenses



现在我这样做:

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

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

}
}

过滤出正确的实体属于当前月份。

  month //是一个保存编码月份的整数(再次:正确!)

但不知何故代码:

  int tempMonth = [[entity month] intValue]; 

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



与此代码相同:

  for(Expense * entity in monthExpenses){
if([entity day] .intValue == todayNumber.intValue){//这里ENTITY DAY.INTVALUE返回一个完全错误的整数!
[thisDayExpenses addObject:entity];
}
}

我似乎缺少一些东西 -



任何想法?

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


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?

解决方案

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.

这篇关于核心数据整数更改值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆