NSInteger integer and master data storage

I have an integer named marbles, and I'm trying to store it in an array using the following code:

[records setValue:marbles forKey:@"marbles"]; 

With this code, I get a warning:

warning: Semantic Issue: incompatible integer to convert the pointer sending 'int' to a parameter of type 'id'

So how to set the value for NSInteger.

Next question: How to reload the array into the master data? I get an array, make changes and how to apply these changes to Core Data?

thanks

+4
source share
3 answers

Initialize NSNumber (this is what CoreData expects) with your integer:

 NSNumber *marbleNumber = [NSNumber numberWithInt:marbles]; [records setObject:marbleNumber forKey@ "marbles"]; 

Or:

 [records setMarbles:[NSNumber numberWithInt:marbles]]; 

To save your changes, you save your context:

 NSError *error; [myManagedObjectContext save:&error]; //handle your error 
+14
source

NSArrays will only accept objects, so the first step is to turn your NSInteger into NSNumber using this method:

 + (NSNumber *)numberWithInt:(int)value 

So:

 NSNumber *myNumber = [NSNumber numberWithInt:marbles]; 

and then you can:

 [records setValue:myNumber forKey:@"marbles"]; 

Basically, as soon as you retrieve the data, you get a managed ObjectContext object, consider it a drawing board, and any changes (including adding or deleting new objects) that you make for these objects can be saved again in CoreData using something like this

 NSError *error; if (![context save:&error]) { // Update to handle the error appropriately. NSLog(@"Unresolved error %@, %@", error, [error userInfo]); exit(-1); // Fail } 

Where context is the context that you will get with NSFetchedResultsController. What you can do like this:

 NSManagedObjectContext *context = [fetchedResultsController managedObjectContext]; 

I would recommend taking a look at the Master Data Programming Guide.

+6
source
 - (id)primitiveValueForKey:(NSString *)key; - (void)setPrimitiveValue:(id)value forKey:(NSString *)key; 

use NSNumber instead of value (id)

0
source

Source: https://habr.com/ru/post/1380874/


All Articles