Zakaria Darwish
Zakaria Darwish

Reputation: 358

convert NSString to int16_t and insert them inside core data

hello what i am trying to do is to taking a NSString value from UITextField and convert this value into int16_t to save it inside my core data after many hour of googling some of the stack people said that core data could not accept primitive data type such as int Boolean etc... and should receive object type such as NSNumber NSInteger etc... here is my code:

                 CoreData *coreDat  = [CoreData defaultStack];

    numberEntity *entry =  [NSEntityDescription insertNewObjectForEntityForName:@"NumberEntity" inManagedObjectContext:coreDat.managedObjectContext]
    ;





    [entry setNumber:(NSNumber *)self.numberTextField.text];

and here is the entity class:

numberEntity.m

        #import "numberEntity.h"
        @implementation numberEntity
        @dynamic  number;
        @end

numberEntity.h

        #import <Foundation/Foundation.h>
        @interface numberEntity : NSObject
        @property (nonatomic,strong) NSNumber *number;
        @end

and for my core data is integer 16

i could not post the image due to my low reputation i am so sorry also i am still new to stackoverflow.com

but after running this code i got this exception :

Unacceptable type of value for attribute: property = "number"; desired type = NSNumber; given type = __NSCFString; value = 1.

hope you will help me forget to mention that i am still newbie in IOS development and i came from android development

Thanks in advance

Upvotes: 0

Views: 495

Answers (1)

Vishnuvardhan
Vishnuvardhan

Reputation: 5107

Just replace this line [entry setNumber:(NSNumber *)self.numberTextField.text]; to [entry setNumber:@([self.numberTextField.text intValue])]; .Hope its working.

Upvotes: 1

Related Questions