核心数据不能正确保存整数?从NSString更改为int时

时间:2022-01-06 16:00:56

I'm using Core data on my app ok,

我正在使用我的应用程序上的核心数据,

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

保存一个应该是整数的数字时出现问题...当我从Core Data中获取它时,它显示奇怪的数值

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 to 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

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转换为int,从字符串保存

but the correct value 345,, when just sent an int directly,

但正确的值345 ,,直接发送一个int,

2 个解决方案

#1


5  

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字段求解。

#2


2  

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值。

#1


5  

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字段求解。

#2


2  

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值。