开发者

ObjC/Core Data. How can I change the key value of an entity from another NSArrayController?

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 ano开发者_StackOverflowther 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.


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"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜