RGriffiths
RGriffiths

Reputation: 5970

Difficulty writing number value back to Core Data

I have a Core Data entity called Status with two attributes savedMessageID as Integer32 and savedMessageText as String.

I have a string stored in an SQL database which the app downloads on startup. The string from the database is broken down into two parts ID and text:

    NSInteger *newMessageID = [the_ID_as_a_string integerValue];
    NSString *newMessageText = the_message_as_a_string;

I then read the Core Data record and compare the IDs to see if there has been a change. If the downloaded ID is greater than the stored ID the new message is displayed. This is working fine so far.

I then try to write back to Core Data with the new downloaded ID. Here is the problem ... much talked about on here already I know:

NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"Status" inManagedObjectContext:context];
NSManagedObject *updatedStatus = [[NSManagedObject alloc]initWithEntity:entitydesc insertIntoManagedObjectContext:context];
[updatedStatus setValue:newMessageID forKey:@"savedMessageID"]; //**error line**
[updatedStatus setValue:newMessageText forKey:@"savedMessageText"];
NSError *error;
[context save:&error];

It seems I can't write back an NSInteger back to the Core Data . I have tried writing NSNumber, int etc but all of these flag up issues. Any ideas how I should do this?

** The error I am getting is *implicit conversion of non-Objective-C pointer to NSInteger *' (aka 'int ') to 'id' is disallowed with ARC

Upvotes: 0

Views: 66

Answers (1)

thom_ek
thom_ek

Reputation: 671

You’ve made a mistake. Property [the_ID_as_a_string integerValue] returns integer value, not a pointer to it. Properly is:

NSInteger newMessageID = [the_ID_as_a_string integerValue];

and then setting value (expected is NSNumber):

[updatedStatus setValue:@(newMessageID) forKey:@"savedMessageID"];

or

[updatedStatus setValue:[NSNumber numberWithInteger:newMessageID] forKey:@"savedMessageID"];

I suggest you to generate strict models to avoid type mismatch (see answers here How to create classes after creating Core Data model in Xcode for iPhone).

Upvotes: 3

Related Questions