[英]Core Data not saving integer properly? When changing from NSString to int
I'm using Core data on my app ok, 我在我的应用程序上使用Core数据,
i have a problem when saving a number that is supposed to be an integer... when I fetch it from Core Data, it shows strange number value 保存一个应该是整数的数字时我遇到问题...当我从核心数据中获取它时,它显示了奇怪的数字值
I know is working because, with the test, having my idTosave = 345, it shows on results of CoreData, but when sending a value [string to int] that I know is woking as it logs the correct response, CD saves a strange value: 我知道这是可行的,因为在测试中,我的idTosave = 345,它显示在CoreData的结果上,但是当发送一个值[string到int]时,由于它记录了正确的响应,我知道它正在唤醒,CD保存了一个奇怪的值:
(Please note Im saving to entity idRef) (请注意我正在保存到实体idRef)
NSLog(@"resultToDisplay:: %@", self.resultsToDisplay);
// int idToSave = 345; //if I use this line instead of intValue it saves 345 OK!
int idToSave = (int)[self.resultsToDisplay intValue];
NSLog(@"id salvada as NSINTEGER:: %d",idToSave);
//graba a CD!
[Student insertWithDataQR:[studentDataDicto objectForKey:@"name"] surname:[studentDataDicto objectForKey:@"surname"]
phone:[studentDataDicto objectForKey:@""]
dob:[studentDataDicto objectForKey:@"dob"] idRef:idToSave ...]];
[[Student defaultManagedObjectContext]save:nil];
log out: 登出:
resultToDisplay:: 1329955200
2012-02-26 00:36:08.597 MyGym_iPad[10867:707] id salvada as NSINTEGER:: 1329955200
note the value 1329955200, is correctly showed as integer and string before saving, 请注意,值1329955200在保存之前正确显示为整数和字符串,
and to check the response I do: 并检查我的反应:
NSArray *studiantes = [Student all];
for ( Student *tud in studiantes) {
NSLog(@"tu %@ ...%@",tud.studentName, tud.idRef );
}
so, with int 345, i see on the log tu.idRef == 345 因此,使用int 345,我在日志中看到tu.idRef == 345
but with the int from string, i see on the log tu.idRef == -32384 但是从字符串int,我在日志中看到tu.idRef == -32384
so If I save from string that goes to int, I get on my response: -32384 因此,如果我从转到int的字符串中保存,则会得到响应:-32384
edit: 编辑:
I have subclassed my generated core data class manager file, to save idRef as NSNumber 我已将生成的核心数据类管理器文件子类化,以将idRef保存为NSNumber
studentData.idRef = [NSNumber numberWithInt:idRef];
edit x2 编辑x2
to check the log i do: 检查我做的日志:
NSArray *studiantes = [Student all];
for ( Student *tud in studiantes) {
NSLog(@"tu %@ ...%d",tud.studentName, [tud.idRef intValue]);
}
but the result to that is: -32384 ,, for my value casted to int, when saved from string 但是结果是:-32384,对于从字符串保存时我的值强制转换为int
but the correct value 345,, when just sent an int directly, 但是当直接发送int时,正确的值345是,
You have to put it in a NSNumber: 您必须将其放入NSNumber中:
NSNumber *idToSave = [NSNumber numberWithInteger:[self.resultsToDisplay integerValue]];
EDIT 编辑
Actually we've reached the max value, solved with an integer 32 field. 实际上,我们已经达到了最大值,并使用32位整数字段进行了求解。
int
values can not be Objective C id
objects. int
值不能是Objective C id
对象。
Either convert your int
to a NSString
object or, better than that, use NSNumber
objects to contain your int
value. 要么将
int
转换为NSString
对象,要么更好的方法是使用NSNumber
对象包含int
值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.