vendredi 10 juin 2016

Unable to store NSNumber in core data


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.


Aucun commentaire:

Enregistrer un commentaire