SetValue NSManagedObject task (master data)

I want to edit an existing entry in the master data. At the moment I have this code, but it creates a new record (and inserts the correct data into the correct column):

NSManagedObjectContext *context = [[NSApp delegate] managedObjectContext];
NSManagedObject *instrument  = nil;



instrument = [NSEntityDescription insertNewObjectForEntityForName: @"Instrument"
                  inManagedObjectContext: context];

[instrument setValue:[NSNumber numberWithInt:quantityInStockInstruments] 
    forKey: @"quantity"];

The result will look like this:

Instrument | Value | Quantity

Violin     | £25   | 9

           |       | 8 <<< This is the new record that is created, instead of setting the
                           quantity of violin from '9' to '8'

I want the program to edit the column for the amount of the row currently selected (in this case, the row is “fiddle.” How would I do this?

+3
source share
2 answers

As refulgentis said, the key is in the name of the selector. You are adding a new object.

, , - selectedObjects NSArrayController. ( , Ive ):

// Get the selected objects from the NSArrayController.
// There may be more than one object selected, so this needs to be accounted for.
NSArray *selectedObjectsArray = [yourArrayController selectedObjects];

// Get the first object in the array, this is the one that will have it values changed.
id firstSelectedObject = [selectedObjectsArray objectAtIndex:0];

// Change a value in a KVC compliant way
[firstSelectedObject setValue:newValue forKey:@"keyValueToChange"];

Interface Builder?

, . Heres a , , .

+4

: "insert : inContext".

Amrox, , ( Core Data) . , , , iPhone ( ), [[yourDataArray objectAtIndex: [table selectedRow ]] setValue: @ "whatever" forKey: @ "whatever" ]

+1

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


All Articles