views:

105

answers:

1

Hi, I am completely noob in objective-c and I am trying the whole day to find a solution to this problem:
I have two entities (Storage and Expenses).
In Storage I have an attribute "number".
In Expenses I have an attribute "number" and another called "cost"
Both entities are linked to tables via cocoa-bindings
I have created subclasses for both NSArrayControllers so I can create the following action:
1. multiply the Expenses.selection.number with the Storage.selection.cost
I did this with this code in the 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 Storage.selection.number and add the Expenses.selection.number
here I have a problem. In the expenses ArrayController I try to:

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 to create a method in the Storage ArrayController:

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

but when I call it like: [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 doesn't return any error message, it doesn't work.
any ideas how to solve this problem? thanks a lot.

+1  A: 

There are two ways to do it:

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

Grab the second object from the first object as follows:

id selection = [self selection];
id storage = [selection valueForKey:@"storage"];
[storage setValue:sum forKey:@"number"];
Marcus S. Zarra
omg, all this time it was so easy?? thanks a lot for your help.. :)
Pr301