ObjC / Master Data. How to change the key value of an object from another NSArrayController?

I am completely noob in objective-c, and I have been trying all day to find a solution to this problem:
I have two objects (Storage and expenses).

In the repository, I have a number attribute.

In expenses I have the attribute “number” and the other is called “cost”
Both objects are connected to tables via cocoa -bindings
I have subclassed NSArrayControllers, so I can create the following action:
1. Multiply the number Expenses.selection.number using Storage .selection.cost
I did this with this code in Expenses ArrayController:

- (IBAction)submit:(id)sender {
    NSNumber *price = [NSNumber numberWithFloat:[[self.selection valueForKey:@"storage.buy"] floatValue]];
    NSNumber *insert = [NSNumber numberWithInt:[[self.selection valueForKey:@"number"] intValue]];
    NSNumber *cost;
    cost = [NSNumber numberWithFloat:[insert intValue] * [price floatValue]];
    [self.selection setValue:cost forKey:@"cost"];


2. get the file Storage.selection.number and add Expenses.selection.number
here I have a problem. In ArrayController expenses I try:

NSNumber *stock = [NSNumber numberWithInt:[[self.selection valueForKey:@"storage.number"] intValue]];
sum = [NSNumber numberWithInt:[stock intValue] + [insert intValue]];
[self.selection setValue:sum forKey: @"storage.number"];


but I get: [<NSManagedObject 0x10013ad50> setValue:forUndefinedKey:]: the entity Expenses is not key value coding-compliant for the key "Storage.number".

I also tried creating a method in Storage ArrayController:

-(void)setNumber:(NSNumber*)number{
    [self.selection setValue:number forKey: @"number"];
}

but when I call it this: [Storage seNumber: sum];it returns:

+[Storage setNumber:]: unrecognized selector sent to class 0x100003430

I also tried:

Storage *st = [[Storage alloc] init];
    [st setNumber: sum];
    [st release];


and although it does not return an error message, it does not work.
any ideas how to solve this problem? Many thanks.

+3
source share
1 answer

There are two ways to do this:

Use -setValue: forKeyPath:instead -setValue: forKey:; or

Move the second object from the first object as follows:

id selection = [self selection];
id storage = [selection valueForKey:@"storage"];
[storage setValue:sum forKey:@"number"];
+2
source

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


All Articles