Unable to store NSNumber in core data

Posted by Kamlesh on Stack Overflow See other posts from Stack Overflow or by Kamlesh
Published on 2011-01-04T12:50:17Z Indexed on 2011/01/04 12:54 UTC
Read the original article Hit count: 296

Filed under:
|

Hi all, 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.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about ios