3
votes

I am using Core Data in my application.I have an attribute named PINCode which has property Int64. Now,in my app I take the PIN code from a text field,convert it into NSNumber and try to store as an attribute value for an entity.But I am unable to do so.The exception that it shows on the console is:- Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for attribute: property = "PINCode"; desired type = NSNumber; given type = NSCFString; value = 121.'

Here is the code:- (Conversion to NSNumber):-

NSString *str= txtPINCode.text;
NSNumber *num = [[NSNumber alloc]init];
int tempNum = [str intValue];
num = [NSNumber numberWithInt:tempNum];

(Storing in core data entity):-

NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newContact;
newContact = [NSEntityDescription insertNewObjectForEntityForName:@"MemberTable" inManagedObjectContext:context];
[newContact setValue:num forKey:@"PINCode"];

The app crashes at this point.I am unable to find the reason of crash.I have also tried to check the conversion by the following code:-

NSNumber *testNumber = [[NSNumber alloc]init];
id test = num;
BOOL testMe = [test isKindOfClass:(Class)testNumber];

Can anybody help me with this?? Thanks in advance.

1
Have you tried changing the attribute type for PINCode to Int 32? You shouldn't need the extra precision of 64 bits on a PIN code field, and the 32-bit iPhone processor might be representing the 64 bit type differently with Core Data.Brad Larson
You can't just typecast a class instance to a class, like you do in that last line there. You need to use [testNumber class] for that. In any case, I'm not sure what you're trying to do with the type comparison.Brad Larson
Also, you're leaking an NSNumber instance on your second line of code above, because you allocate a number, then never release it and overwrite it two lines later.Brad Larson

1 Answers

0
votes

Well, the first code block is a bit strange. In line 2 you allocate an NSNumber then in line 4 you leak that NSNumber from line 2 and get a new NSNumber object (which gets autoreleased).

So, when you come to second code block, how are those code blocks related? num (from code block 1) could point to trash in code block 2.

Try to delete line 2 of code block 1 and in old line 4 write the following:

NSNumber *num = [[NSNumber alloc] initWithInt:tempNum];

Do not forget to release that later on.